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

Partials don't work #1

Closed
ilmich opened this issue May 12, 2010 · 9 comments
Closed

Partials don't work #1

ilmich opened this issue May 12, 2010 · 9 comments

Comments

@ilmich
Copy link

ilmich commented May 12, 2010

Hi, I don't know if there is something wrong in my test code, but the partials seems don't work properly.

This is my test code: http://pastebin.com/FDsmbch5
The output of this script, don't show the list of childs.

Whit this stupid patch http://pastebin.com/tSgaN6mu partials works fine.
Seems that when rendering a partial, the context is encapsulated recursively in another array.
Greetings

@bobthecow
Copy link
Owner

Thanks. This should be fixed in master now. Let me know if you still have problems.

@bobthecow
Copy link
Owner

Further explanation:

Contexts weren't always stored this way internally, and partials must have broken when it switched over.

Apparently I need more test cases for partials :)

The context is always encapsulated in an array (inside a Mustache instance) because a context is actually a cascading set of contexts. For example, inside an iterable block, the context will contain both the original template context, as well as the context for the current $i in the foreach loop. If a variable is present in the current context, that one is rendered, else it is looked for in the parent context.

Your patch works fine in instances where only one context exists. To solve the general case of the partials problem, however, the set of contexts needs to be flattened before passing into the partial's view. This is done by the new Mustache::_flattenContext() call.

@ilmich
Copy link
Author

ilmich commented May 17, 2010

Thanks for the quick solution and explanation :)
But I have another little question.
Why in the _flattenContext() method, you check if the context is an instance of class Mustache and not simply an object, as in the rest of the code?
Maybe it's a bad choice, but during my tests I also passed, as root context, a complex object of another type.
And it was very convenient for me, because I did not have to load the data into the template, and I still managed to keep reusability of the code.
Greetings

@bobthecow
Copy link
Owner

I'm hesitant to set properties on an object of an arbitrary class. If the class implements __get() and __set(), trying to flatten properties of sub-contexts into the root context can have unforeseen side effects.

I made the assumption that the root context was either a Mustache instance ($this) or a context array passed to the constructor or render() method. You bring up a good point, that the object passed may very well be an instance of some arbitrary class.

Let me look into this and get back to you, k?

@bobthecow
Copy link
Owner

Okay. I think eeb5b24 might be just the thing you need. Wanna do me a favor and check out the feature/fix-partials branch and see if that fixes your problem? (I have a test case added for this, it seems to work now).

@bobthecow
Copy link
Owner

Never mind, that breaks context in partials... I'm going to take another shot at it.

@bobthecow
Copy link
Owner

Okay. I think I got it this time. Check out feature/fix-partials and let me know if that works for you. If so, I'll merge it into dev/master :)

@ilmich
Copy link
Author

ilmich commented May 24, 2010

Wow, great work!!!
My code works without any refactoring!!!
Thank you, I think that this is a very nice feature!!!!

@bobthecow
Copy link
Owner

Great :)

I'll mark this as closed and merge the fix into master. Thanks for your help.

strongwazz referenced this issue in UberGroupLimited/mustache.php Jul 8, 2013
scr4bble added a commit to scr4bble/mustache.php that referenced this issue Jan 17, 2022
Solving PHP 8.1 deprecation warning: htmlspecialchars(): Passing null to parameter bobthecow#1 ($string) of type string is deprecated
This issue was closed.
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