繁体   English   中英

车把:如何将@index作为参数传递给自定义助手

[英]Handlebars: How pass @index as parameter to custom helper

设定:

  • 我正在使用Handelbars.PHP ,但是考虑到Handlebars.js的解决方案可能会对我有所帮助。
  • 定制帮助程序content ,需要两个参数一个集合和一个索引。
  • #each帮助程序调用,它调用#content帮助程序,如下所示:

    {{#each data}} {{#content ../parent_options @index}} {{/ content}} {{/ each}}

问题:

content助手定义中,我想访问@index值,例如0,1,2,...但是我只得到一个字符串!

如何将@index的值作为参数传递并访问内容助手定义内?

正如Everplays在官方项目仓库中评论boukeversteegh评论的那样:

Handlebars.PHP不会解析帮助程序的参数,而是要解析它们并为其赋予含义(我正在谈论传递给帮助程序的第三个参数, _helper_each($template, $context, $args, $source)) 但是在您的情况下,无需解析参数,您只需通过$context->lastIndex()

完整评论位于https://github.com/XaminProject/handlebars.php/issues/27

最终的定制帮助程序供参考:

    /**
     * Handlebars Helper Content. Return object->Alphabet[index].
     *
     * @param $template
     * @param $context
     * @param $args
     * @param $source
     *
     * @return Handlebars_String
     * @throws InvalidArgumentException
     */
    public static function _helper_content($template, $context, $args, $source) {
        $Alphabet = range('A', 'Z');

        $tmp = explode(' ', $args);
        $buffer = '';

        $object = $context->get($tmp[0]);

        if (count($tmp) < 2 || $tmp[1] == '@index') {
            $last_index = $context->lastIndex();
            $letter = $Alphabet[$last_index];
        }
        else {
            $index = $context->get($tmp[1]);
            $letter = $Alphabet[$index];
        }

        $buffer .= $object->$letter;

        return new Handlebars_String($buffer);
    }

以下是一些样板代码,您可以使用它们创建带有自定义帮助程序的强大模板,同时仍使用少量代码。

它包含两个助手:

  • 一个将字符串更改为大写的字符。 在演示代码@key值传递给它。
  • 一个递增数值。 在演示代码@index值传递给它。

模板table.hbs

{{#if table}}
<table>
    <thead>
        <tr>
            <th style="width: 2%;">#</th>
            {{#each table.0}}
            <th nowrap="nowrap">
                {{uppercase @key}}
            </th>
            {{/each}}
        </tr>
    </thead>
    <tbody>
        {{#each table}}
        <tr> 
            <th>{{increment @index}}</th>
            {{#each this}}
            <td><input type="text" value="{{this}}" /></td>
            {{/each}}
        </tr>
        {{/each}}
    </tbody>
</table>
{{#if paginator}}
<nav class="pagination">
    <ul>
        {{#each paginator}}
        <li><a href="{{this}}">{{@key}}</a></li>
        {{/each}}
    </ul>
</nav>
{{/if}}
{{else}}
<p>No data found!</p>
{{/if}}

风景 :

// Create handlebars object
$template_engine = new \Handlebars\Handlebars;

// Get helpers object
$helpers = $template_engine->getHelpers();

// Add 'camelize' helper
$helpers->add('uppercase', function($template, $context, $args, $source) {
    return ucwords($context->get($args));
});

// Add 'increment' helper
$helpers->add('increment', function($template, $context, $args, $source) {
    return $context->get($args) + 1;
});

// Render template
echo $template_engine->render(file_get_contents(__DIR__ . '/table.hbs'), array(
    'table' => array(
        array('name' => 'John', 'profession' => 'programmer', 'city' => 'Leuven'),
        array('name' => 'Sam', 'profession' => 'designer', 'city' => 'Aarschot'),
        array('name' => 'Tom', 'profession' => 'owner', 'city' => 'Leuven'),
        array('name' => 'Steve', 'profession' => 'copywriter', 'city' => 'Brussels'),
        array('name' => 'Thomas', 'profession' => 'designer', 'city' => 'Antwerp')
    ),
    'paginator' => array(
        'first' => 'http://www.test.com/first',
        '1' => 'http://www.test.com/1',
        '2' => 'http://www.test.com/2',
        '3' => 'http://www.test.com/3',
        'last' => 'http://www.test.com/last'
    )
));

输出 :

<table>
    <thead>
        <tr>
            <th style="width: 2%;">#</th>
            <th nowrap="nowrap">
                Name
            </th>
            <th nowrap="nowrap">
                Profession
            </th>
            <th nowrap="nowrap">
                City
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>1</th>
            <td><input type="text" value="John" /></td>
            <td><input type="text" value="programmer" /></td>
            <td><input type="text" value="Leuven" /></td>
        </tr>
        <tr>
            <th>2</th>
            <td><input type="text" value="Sam" /></td>
            <td><input type="text" value="designer" /></td>
            <td><input type="text" value="Aarschot" /></td>
        </tr>
        <tr>
            <th>3</th>
            <td><input type="text" value="Tom" /></td>
            <td><input type="text" value="owner" /></td>
            <td><input type="text" value="Leuven" /></td>
        </tr>
        <tr>
            <th>4</th>
            <td><input type="text" value="Steve" /></td>
            <td><input type="text" value="copywriter" /></td>
            <td><input type="text" value="Brussels" /></td>
        </tr>
        <tr>
            <th>5</th>
            <td><input type="text" value="Thomas" /></td>
            <td><input type="text" value="designer" /></td>
            <td><input type="text" value="Antwerp" /></td>
        </tr>
    </tbody>
</table>
<nav class="pagination">
    <ul>
        <li><a href="http://www.test.com/first">first</a></li>
        <li><a href="http://www.test.com/1">1</a></li>
        <li><a href="http://www.test.com/2">2</a></li>
        <li><a href="http://www.test.com/3">3</a></li>
        <li><a href="http://www.test.com/last">last</a></li>
    </ul>
</nav>

暂无
暂无

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

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