Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper does not work in array? #173

Closed
phoenixg opened this issue Nov 14, 2013 · 5 comments
Closed

Helper does not work in array? #173

phoenixg opened this issue Nov 14, 2013 · 5 comments

Comments

@phoenixg
Copy link

// initialize
$this->mustache = new Mustache_Engine(array(
    'helpers' => array(
        'i18n' => function($text) {
            return $text;
        },
        'nl2br' => function($text) {
            return nl2br($text);
        }
    ),
));

// template : this not work
{{# para }}
    {{ originAddress }}
    {{ originDate }}
    {{ title }}
    {{#nl2br}}{{ english }}{{/nl2br}}
{{/ para }}

// template : this would work
{{#nl2br}}{{ variablename }}{{/nl2br}}

How to solve?

@bobthecow
Copy link
Owner

Can you show me your data too?

@phoenixg
Copy link
Author

Here it is, originally, it's parsed from JSON.

print_r($satires);die;
Array
(
    [0] => Array
        (
            [originAddress] => http://comment.news.163.com/news_guonei8_bbs/fefefefefefe.html
            [originDate] => 
            [title] => 
            [english] => aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, 
cccccccccccccccccccccccccccccccccccccccccccc, 
dddddddddddddddddddddddddddddddddddddd, 
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee, 
fffffffffffffffffffffffffffffffffffff.
            [tags] => Array
                (
                )

        )

    [1] => Array
        (
            [originAddress] => a
            [originDate] => aa
            [title] => aaa
            [english] => aaaa
            [chinese] => aaaaa
            [tags] => Array
                (
                )

        )

)

@bobthecow
Copy link
Owner

Ahh. I think i know what is happening. Try changing your nl2br helper:

function($text, $mustache) {
    return nl2br($mustache->render($text));
}

@phoenixg
Copy link
Author

@bobthecow Great! It work, wonder why?

@bobthecow
Copy link
Owner

It works because of how the Mustache spec treats "lambda sections", which is what helpers actually are. In your case, it's this part of the template:

{{# nl2br }}{{ english }}{{/ nl2br }}

Per the spec, the raw template contents is sent to the helper. So it's calling the helper with the contents of that section:

{{ english }}

Note that this isn't rendered yet. So you were calling nl2br on "{{ english }}", which, of course, wasn't what you wanted at all. By using the $mustache engine passed in to the helper as the second param, you can pre-render the "{{ english }}" into what it should be: $mustache->render($text).

Then you'd call nl2br on the rendered text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants