Hi everyone,

Today, I introduce an idea to assign default values to $template in your
template throgh view's attributes.

This can be implemented simply like this:

// ProjectBaseView.class.php
<?php
class ProjectBaseView extends AgaviView
{
  public function loadLayout($layoutName = null)
  {
    $parameters = parent::loadLayout($layoutName);
    $assigns = array_key_exists('default_assigns', $parameters) ?
(array)$parameters['default_assigns'] : array();
    foreach( $assigns as $key=>$value) {
      $this->setAttribute($key, $value);
    }
    return $parameters;
  }

  /**
    some codes here....
  **/
}
?>
// EOF

Only this :)
And you can set default values following:
// a layout setting in output_type.xml
<layout>
  <layers>
    <layer name="content" />
    <layer name="decorator">
      <parameter name="template">%core.template_dir%/Master</parameter>
    </layer>
  </layers>

  <parameter name="default_assigns">
    <parameter name="allow_index">false</parameter>
    <parameter name="author">MugeSo</parameter>
    <parameter name="style_sheets"><!-- an array parameter -->
      <parameter>css/base.css</parameter>
      <parameter>css/default.css</parameter>
    </parameter>
  </parameter>
</layout>
// End of layout

Now you can use these values in your templates ;)
For example, using them in Master template:
// Master.php
<html>
  <head>
        <meta name="author" content="<?php echo $template['author']; ?>" />
        <meta name="robots" content="<?php if($template['allow_index']):
?>index<?php else: ?>noindex<?php endif;?>,follow" />

        <?php foreach($template['style_sheets'] as $style_sheet): ?>
        <link rel="stylesheet" href="<?php echo $style_sheet; ? />"
type="text/css">
        <?php endforeach; ?>
        <base href="<?php echo $ro->getBaseHref();?>" />
  </head>
  <body>
<?php echo $inner; ?>

  </body>
</html>
// EOF

Above code works without any notice even if you don't set author,
allow_index or style_sheets in your View, though you needed to set them
in  all of your Views or to check $template with
isset($template['some_key']) or array_key_exists('some_key', $template)
in your template before.

Of course, you are also allowed to overwrite some of them like this:
// executeHtml in your view
function executeHtml(AgaviRequestDataHolder $rd)
{
    $this->loadLayout();// you can use setupHtml here too ;)
    $this->setAttribute('allow_index', true);
    $this->setAttribute('author', 'David Zulke');
    $this->appendAttribute('style_sheets', 'css/other.css');

    /** some codes here **/
}
// end of executeHtml

Thank for your reading :)

enjoy your weekend,

from Japan,

MugeSo

_______________________________________________
users mailing list
[email protected]
http://lists.agavi.org/mailman/listinfo/users

Reply via email to