Exemple #1
0
 public function getTemplate()
 {
     $tpl_name = $this->getPropInherited('visual.template');
     if (!$tpl_name) {
         return false;
     }
     $parts = explode(":", $tpl_name);
     $tpl = \Floxim\Floxim\Template\Loader::loadTemplateVariant($parts[0], $parts[1]);
     // Assign template into env if this infoblock is the layout infoblock
     if ($this['controller'] === 'layout' && $this['action'] === 'show') {
         fx::env('theme_template', $tpl);
     }
     return $tpl;
 }
Exemple #2
0
 public static function template($template_name = null, $data = array())
 {
     if (func_num_args() == 0) {
         return new Template\Loader();
     }
     $parts = explode(":", $template_name);
     if (count($parts) == 2) {
         $template_name = $parts[0];
         $action = $parts[1];
     } else {
         $action = null;
     }
     if (!preg_match("~^@~", $template_name)) {
         $template_name = self::getComponentFullName($template_name);
     }
     $template = Template\Loader::loadByName($template_name, $action, $data);
     return $template;
 }
Exemple #3
0
 protected function tokenCallToCode(Token $token)
 {
     $each = $token->getProp('each');
     if ($each) {
         $each_token = Token::create('{each}');
         $each_token->setProp('select', $each);
         $item = '$' . $this->varialize($each) . '_item';
         $each_token->setProp('as', $item);
         $token->setProp('each', '');
         $each_token->addChild($token);
         return $this->tokenEachToCode($each_token);
     }
     $code = "<?php\n";
     $is_apply = $token->getProp('apply');
     $tpl_name = $token->getProp('id');
     $tpl = '$tpl_' . $this->varialize($tpl_name);
     $call_children = $token->getChildren();
     /*
      * Converted:
      * {call id="wrap"}<div>Something</div>{/call}
      * like this:
      * {call id="wrap"}{var id="content"}<div>Something</div>{/var}{/call}
      */
     $has_content_param = false;
     foreach ($call_children as $call_child) {
         if ($call_child->name == 'code' && $call_child->isEmpty()) {
             continue;
         }
         if ($call_child->name != 'var') {
             $has_content_param = true;
             break;
         }
     }
     if ($has_content_param) {
         $token->clearChildren();
         $var_token = new Token('var', 'single', array('id' => 'content'));
         foreach ($call_children as $call_child) {
             $var_token->addChild($call_child);
         }
         $token->addChild($var_token);
     }
     $with_expr = $token->getProp('with');
     if ($with_expr) {
         $ep = new ExpressionParser();
         $with_expr = $ep->parseWith($with_expr);
     }
     $switch_context = is_array($with_expr) && isset($with_expr['$']);
     // && !$is_apply;
     if ($switch_context) {
         $new_context_expression = $this->parseExpression($with_expr['$']);
     }
     $passed_vars = array();
     if (is_array($with_expr)) {
         foreach ($with_expr as $alias => $var) {
             if ($alias == '$') {
                 continue;
             }
             $passed_vars[trim($alias, '$')] = array('string', $this->parseExpression($var));
         }
     }
     foreach ($token->getChildren() as $param_var_token) {
         // internal call only handle var
         if ($param_var_token->name != 'var') {
             continue;
         }
         $value_to_set = '';
         if ($param_var_token->hasChildren()) {
             // pass the inner html code
             $value_to_set .= "ob_start();\n";
             $value_to_set .= $this->childrenToCode($param_var_token);
             $value_to_set .= "\n";
             $passed_value_type = 'buffer';
         } elseif ($select_att = $param_var_token->getProp('select')) {
             // pass the result of executing the php code
             $value_to_set = self::parseExpression($select_att);
             $passed_value_type = 'string';
         }
         $passed_vars[$param_var_token->getProp('id')] = array($passed_value_type, $value_to_set);
     }
     $switch_context_local = $switch_context && count($passed_vars) > 0;
     if ($is_apply) {
         $context_var = '$context';
     } else {
         if (count($passed_vars) > 0 || $switch_context) {
             $context_var = $tpl . '_context';
             $code .= $context_var . " = new \\Floxim\\Floxim\\Template\\" . fx::config('templates.context_class') . "();\n";
         } else {
             $context_var = 'new \\Floxim\\Floxim\\Template\\' . fx::config('templates.context_class') . '()';
         }
     }
     // switch context to calculate passed vars inside it
     if ($switch_context_local) {
         $code .= '$context->push(' . $new_context_expression . ");\n";
     }
     if (count($passed_vars) > 0) {
         $tpl_passed = $tpl . "_passed";
         $code .= $tpl_passed . " = array();\n";
         foreach ($passed_vars as $passed_var_key => $passed_var) {
             switch ($passed_var[0]) {
                 case 'string':
                 default:
                     $code .= $tpl_passed . "['" . $passed_var_key . "'] = " . $passed_var[1] . ";\n";
                     break;
                 case 'buffer':
                     $code .= $passed_var[1];
                     $code .= $tpl_passed . "['" . $passed_var_key . "'] = ob_get_clean();\n";
             }
         }
         // passed vars calculated, clear context
         if ($switch_context_local) {
             $code .= "\$context->pop();\n";
         }
     }
     if ($switch_context) {
         $code .= $context_var . "->push(" . $new_context_expression . ");\n";
     }
     if (isset($tpl_passed)) {
         $code .= $context_var . "->push(" . $tpl_passed . ", array('transparent' => true));\n";
     }
     // ------------
     $tpl_name_is_expression = !preg_match("~^[a-z0-9_\\,\\.\\:\\@\\#]+\$~", $tpl_name);
     $loader = "\\Floxim\\Floxim\\Template\\Loader";
     // not a plain name
     if ($tpl_name_is_expression) {
         $tpl_name = self::parseExpression($tpl_name);
         $pn = $tpl . '_parsed';
         $code .= $pn . ' = ' . $loader . '::parseTemplateName(' . $tpl_name . ', ' . var_export($this->template_set_name, 1) . ', ' . var_export($this->is_aliased, 1) . ");\n";
         $code .= $tpl . " = ";
         $code .= $loader . "::loadTemplateVariant(" . $pn . '["group"], ' . $pn . '["action"], ' . $context_var . ', ' . $pn . '["forced_group"], ' . $pn . '["tags"]); ' . "\n";
     } else {
         $parsed_name = \Floxim\Floxim\Template\Loader::parseTemplateName($tpl_name, $this->template_set_name, $this->is_aliased);
         foreach ($parsed_name as &$v) {
             $v = var_export($v, 1);
         }
         $code .= $tpl . " = ";
         $code .= $loader . "::loadTemplateVariant(" . $parsed_name['group'] . ", " . $parsed_name['action'] . ", " . $context_var . ", " . $parsed_name['forced_group'] . ', ' . $parsed_name['tags'] . ");\n";
     }
     /*
     if (!preg_match("~[\:\@]~", $tpl_name)) {
        $tpl_name = $this->template_set_name . ":" . $tpl_name;
     }
     
     $tpl_at_parts = explode("@", $tpl_name);
     if (count($tpl_at_parts) === 1) {
         $forced_group = 'null';
         list($set_name, $action_name) = explode(":", $tpl_name);
         // save @ for named ("aliased") template groups (like "@admin")
         if ($set_name === $this->template_set_name && $this->is_aliased) {
             $set_name = '@'.$set_name;
         }
     } else {
         $forced_group = !empty($tpl_at_parts[0]) ? $tpl_at_parts[0] : $this->template_set_name;
         $action_parts = explode(":", $tpl_at_parts[1]);
         if (count($action_parts) === 1) {
                 array_unshift($action_parts, $forced_group);
         }
         list($set_name, $action_name) = $action_parts;
         $forced_group = "'".$forced_group."'";
     }
     $tag_parts = explode("#", $action_name);
     if (count($tag_parts) > 1) {
         $action_name = $tag_parts[0];
         $tags = "array('".join("', '", explode(",", $tag_parts[1]))."')";
     } else {
         $tags = 'null';
     }
     */
     $code .= "if ( " . $tpl . " ) {\n";
     $code .= "echo " . $tpl . "->setParent(\$this)->render();\n";
     if ($subroot_var = $token->getProp('extract_subroot')) {
         $code .= $subroot_var . " = " . $tpl . "->is_subroot;\n";
     }
     $code .= "}\n";
     // ------------
     // clear vars passed into child template from current context
     if ($is_apply && count($passed_vars) > 0) {
         $code .= "\$context->pop();\n";
     }
     // clear context object
     if ($is_apply && $switch_context) {
         $code .= "\$context->pop();\n";
     }
     $code .= "\n?>";
     return $code;
 }