简体   繁体   English

smarty模板变量不清楚

[英]smarty template variable not clear

I am trying to understand developing web apps with PHP and smarty this is the book 我正在尝试了解使用PHP开发Web应用程序, 这是本书

there is a file departments_list.tpl 有一个文件department_list.tpl

{* departments_list.tpl *}
{load_presentation_object filename="departments_list" assign="obj"}
{* Start departments list *}

<div class="box">
  <p class="box-title">Choose a Department</p>
  <ul>
      {* Loop through the list of departments *}
      {section name=i loop=$obj->mDepartments}
        {assign var=selected value=""}
        {* Verify if the department is selected to decide what CSS style to use *}
        {if ($obj->mSelectedDepartment == $obj->mDepartments[i].department_id)}
        {assign var=selected value="class=\"selected\""}
        {/if}
        <li>
        {* Generate a link for a new department in the list *}
        <a {$selected} href="{$obj->mDepartments[i].link_to_department}">
        {$obj->mDepartments[i].name}
        </a>
        </li>
      {/section}
  </ul>
 </div>
{* End departments list *}

I do not understand in above file one line which is 我不明白上面文件中的哪一行是

      {section name=i loop=$obj->mDepartments}

how does name=i works I what is i initialized to and how can a loop start from i, name=i工作方式,初始化的方式以及如何从i开始循环,

secondly above file is using a file function.load_presentation_object.php 其次,上面的文件正在使用文件function.load_presentation_object.php

<?php
// Plug-in functions inside plug-in files must be named: smarty_type_name
function smarty_function_load_presentation_object($params, $smarty)
        {
        require_once PRESENTATION_DIR . $params['filename'] . '.php';
        $className = str_replace(' ', '',ucfirst(str_replace('_', ' ',$params['filename'])));
        // Create presentation object
        $obj = new $className();
        if (method_exists($obj, 'init'))
        {
        $obj->init();
        }
        // Assign template variable
        $smarty->assign($params['assign'], $obj);
        }
?>

in above file a parameter $params has been used in last line 在上面的文件中,最后一行使用了参数$ params

        $smarty->assign($params['assign'], $obj);

as far as I can understand from file departments_list.tpl passes on parameter to function.load_presentation_object.php as 据我从文件department_list.tpl可以理解,将参数传递给function.load_presentation_object.php为

{load_presentation_object filename="departments_list" assign="obj"}

so when following is called 所以当跟随

function smarty_function_load_presentation_object($params, $smarty)

$params=depratments_list and $smarty=obj $ params = depratments_list和$ smarty = obj

so how does $smarty->assign($params['assign'], $obj); 那么如何$ smarty-> assign($ params ['assign'],$ obj);

work what is $params['assign'] in above? 工作上面的$ params ['assign']是什么?

{load_presentation_object filename="departments_list" assign="obj"}

translated to php will be: 转换为php将是:

smarty_function_load_presentation_object(
    array(
        'filename' => 'departments_list', 
        'assign'   => 'obj'
    ), 
    $smarty
);

Every parameters specified in smarty template are passed as associative array in first argument. smarty模板中指定的每个参数都作为关联数组传递给第一个参数。 Second is always current smarty object. 第二是当前最聪明的对象。

As for 至于

{section name=i loop=$obj->mDepartments}

it is simple for loop: 这是简单for循环:

for ($i=0; $i < count($obj->mDepartments); $i++) {
    $current = $obj->mDepartments[$i];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM