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

looping over imported template with a section defination #23

Closed
thevikas opened this issue Oct 7, 2015 · 2 comments
Closed

looping over imported template with a section defination #23

thevikas opened this issue Oct 7, 2015 · 2 comments
Labels

Comments

@thevikas
Copy link
Contributor

thevikas commented Oct 7, 2015

I did a loop (there is no internal loop support, I had to use standard php foreach) and inside the loop I imported by small template. That small template had many sections defined. The content of these sections got exponentially bigger and repeated by every iteration in the final rendered html.

I think, Foil does not understand loops, so it keep the html generated from section of last iteration and keep appending over it. What outputs is like:

  • header
  • imported_section_1
  • imported_section_1
  • imported_section_2
  • imported_section_1
  • imported_section_2
  • imported_section_3
  • footer

when the output should have been

  • header
  • imported_section_1
  • imported_section_2
  • imported_section_3
  • footer

How to fix this behavior?

@gmazzap
Copy link
Contributor

gmazzap commented Oct 7, 2015

@thevikas if I understand well, you want that next item in the loop replace the same section in previous one.

This replace behaviour is not the default in Foil.

If you have a look at documentation http://www.foilphp.it/docs/TEMPLATES/INHERITANCE.html expecially in the section "Functions Summary" you'll see that there're 3 functions to "close" a section: append(), replace() and stop().

As you can guess, append() makes the section content be appended to the content fo same section, if it exists, and replace() make the content above it replace any previous content in the same section.

The stop() behaviour, is a bit different. When used in a layout, it is pretty much used to define a "default" content for the section.

When used in a child template, it acts as append(). However, you can make it act as replace() setting the "section_def_mode" option when you instantiate the Foil engine.

That option is set to SectionInterface::MODE_APPEND but you can change it to SectionInterface::MODE_REPLACE to make stop() acts as replace() by default.

My suggestion is to use stop() in layouts (in file that are wrote to be extended). And in child templates use append() or replace() to make clear what your intention is. This will make you templates better readable.

Regarding loops, Foil has some helpers, have a look here http://www.foilphp.it/docs/FUNCTIONS/LOOP-HELPERS.html.

Helpers linked above may be helpful or not depending your use case, but the usage of PHP core statements and functions (like foreach) is not considered bad at all in Foil, in fact, the whole point of a template engine for PHP templates is to be able to use PHP core constructs.

@thevikas
Copy link
Contributor Author

thevikas commented Oct 7, 2015

Got it. Thanks.

@thevikas thevikas closed this as completed Oct 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants