View previous topic :: View next topic |
Author |
Message |
flexjoly Smarty Rookie
Joined: 08 May 2015 Posts: 15 Location: Apeldoorn, Netherlands
|
Posted: Fri Sep 09, 2016 10:09 pm Post subject: How to use extends with multiple subtemplates? |
|
|
Hi,
I have multiple local sub-templates. Each is parsed inside its own script, with it own local variables.
Each subtemplate can add some parts to the html-head. Like a javascript or stylesheet. Or even change the pagetitle.
To do this the extends with blocks structure is really nice.
Is it possible to have 2 or more sibbling templates extended to one structure of main-extends-templates?
We also have different main-templates. So a subtemplate cannot extends just one static main template. So that needs to be dynamic too.
Is this possible? Or do i need to go back to includes and capture?
Greetz, flexjoly |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Sep 10, 2016 6:51 am Post subject: |
|
|
Can't it be both?
Extension is not a replacement for inclusion. |
|
Back to top |
|
flexjoly Smarty Rookie
Joined: 08 May 2015 Posts: 15 Location: Apeldoorn, Netherlands
|
Posted: Sat Sep 10, 2016 9:09 am Post subject: |
|
|
AnrDaemon wrote: | Can't it be both?
Extension is not a replacement for inclusion. |
I agree...
But i cant figure out, how to do it yet.
The subtemplate has blocks and must be parsed locally and then send to the main template (with extends). So far i cant get it to work.
At first i had:
parent.tpl and subparent.tpl (with extends)
content.tpl extends subparent.tpl with:
{$object->display()}
This breaks the extends and blocks (also tried fetch) |
|
Back to top |
|
flexjoly Smarty Rookie
Joined: 08 May 2015 Posts: 15 Location: Apeldoorn, Netherlands
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Sep 10, 2016 12:24 pm Post subject: |
|
|
flexjoly wrote: | AnrDaemon wrote: | Can't it be both?
Extension is not a replacement for inclusion. |
I agree...
But i cant figure out, how to do it yet.
The subtemplate has blocks and must be parsed locally and then send to the main template (with extends). So far i cant get it to work.
At first i had:
parent.tpl and subparent.tpl (with extends)
content.tpl extends subparent.tpl with:
{$object->display()}
This breaks the extends and blocks (also tried fetch) |
Fix your broken architecture. And the answer will be a simple $smarty->display(). |
|
Back to top |
|
flexjoly Smarty Rookie
Joined: 08 May 2015 Posts: 15 Location: Apeldoorn, Netherlands
|
Posted: Sat Sep 10, 2016 3:29 pm Post subject: |
|
|
My architecture is not broken. Maybe my explanation is confusing. I dont know how to explain it better. I will try again.
Everythings displays just fine. Only the blocks are not captured, but displayed directly.
I can make a working code example but i am afraid it is too large....
I guess the central question is: can you parse subtemplates locally AND let them extend on a (dynamic) main template to add to the defined blocks.
So far i cannot find how i can do that.
I will try to explain:
$child1 = $smarty->createTemplate('child1.tpl')
$child1->assign($arrayParams1)
$child2 = $smarty->createTemplate('child2.tpl')
$child2->assign($arrayParams2)
$main = $smarty->createTemplate('main.tpl')
$main->assign($arrayParamsMain)
How can you extend all childs on the main templ?
A child.tpl does not know in which main.tpl it is used. So it cannot set an {extend}
Maybe an extend-resource-string could be used:
'extends: main.tpl|child1.tpl|child2.tpl'
But this way, you cannot parse the childs locally with their own variables.... |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Sep 10, 2016 4:19 pm Post subject: |
|
|
Simple answer is - don't do that.
If you absolutely must pass a result of one template to another tempplate, then Smarty::fetch() it into a variable and assign it to that other template, but this is the least useful way.
Your architecture is terrible broken. Your only course to save yourself from this damnation, is to fix the way you approach your templates. In the best possible course, the slution should be just $smarty->display() of one template, which includes and/or extends everything that is needed.
If you need custom plugins to format your data, write those plugins. |
|
Back to top |
|
flexjoly Smarty Rookie
Joined: 08 May 2015 Posts: 15 Location: Apeldoorn, Netherlands
|
Posted: Sat Sep 10, 2016 6:02 pm Post subject: |
|
|
Hi AnrDaemon,
Thanks for you patience.
Dont think my architecture is broken. I am no newby....
I implemented smarty2 in our framework. Now I am rewriting our framework and using smarty 3.
To build a nice webpage you need multiple components. So including several templates is good practice.
First i just assigned an object to the parent template, from there it called display(). This way you have a kind of lazy loading.
But that is not working with extends
So indeed i need to go back to fetch().
Still i would like to see that extends can work with multiple local parsed templates.
When smarty doesnt provide.... i will make a workaround
Greetz, flexjoly
a Divaeloper!
nb. using smarty for at least 10 years now....
lately i was forced to use symfony and twig.... glad I can use smarty again |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Sep 10, 2016 7:56 pm Post subject: |
|
|
Make a first step. Assume it is broken. Then explain to yourself, why it is not.
I want to see, how quick you will come to conclusion that your approach is, in fact, terrible broken.
I'm working with a website right now, that uses approach similar to yours (a bunch of separately rendered templates). It's fucking disaster, and I'm slowly going towards a saner system.
I'd still have a few subtemplates manually rendered, but these are static bits of code, like those SVG inserts, but the parts that are an actual content are going to be included/extended directly.
And SVG inserts are going to be wrapped into plugin calls.
The general approach you can find in this example: http://www.smarty.net/forums/viewtopic.php?t=25486
The "page.tpl" will have all the page decorations and stuff, and then if actual rendered content will need additional scripts or CSS, it can append to main template blocks. |
|
Back to top |
|
flexjoly Smarty Rookie
Joined: 08 May 2015 Posts: 15 Location: Apeldoorn, Netherlands
|
Posted: Sat Sep 10, 2016 10:18 pm Post subject: |
|
|
Lets agree to disagree.
I think i saw your post before. That is indeed a bit simular. And i even need more.
One explanation it is not broken is: object orientation. Each object is a black box. It knows itself, but nothing outside itself. It can handle its properties and gives you some output. That's why!
I also liked pages to be clean and of the same structure. But that is not enough anymore. Pages are loaded with blocks of different content. Like a dashboard.
You can structure that by handling these blocks as separate objects (or plugins or widgets). Just use a good framework and it is no disaster anymore! Not a MVC one, but a good one
We build a webpage the way Drupal does: it has blocks to define the layout, inside the block some content is loaded.
So my question still stands
In the meantime I already have some workaround. Maybe even better. I just combine smarty-plugins with singleton-datacontainers to collect data and output them when asked. Also with tiny templates parsed, so i dont have to arrange some html together inside php. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Sep 10, 2016 10:47 pm Post subject: |
|
|
As I said, tell your black box to render itself, make a plugin out of it and register it with Smarty if need.
Then use it in your template as needed.
The "black box" paradigm is nice, when you explain OOP basics to a student, but in reality, it is hardly applicable.
You always have very good idea of how the object works, and the better you know it, the better you can avoid breaking into it.
P.S.
http://lmgtfy.com/?q=why+singleton+is+considered+an+antipattern |
|
Back to top |
|
flexjoly Smarty Rookie
Joined: 08 May 2015 Posts: 15 Location: Apeldoorn, Netherlands
|
Posted: Sun Sep 11, 2016 12:08 pm Post subject: |
|
|
I do agree on singleton being an anti-pattern, but it is very usefull.... although i do hate it.
Saying that OOP is hardly applicable.... i cant disagree more with you on that! Maybe you have seen examples where it was made to complex.
It is a way of thinking. Coding even becomes a lot easier!
When you got it.... and can keep it practical, you never want anything else.
As for everything the truth is: dont over use it, keep it practical.
Indeed the black box renders itself...
Still hoping Smarty makes a solutions to combine extends with local parsed subtemplates (just to remember what the real topic is) |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Sep 11, 2016 8:27 pm Post subject: |
|
|
Nowhere I said that OOP is not applicable.
If your black box renders itself, then Smarty::registerPlugin() its rendering method. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Sep 15, 2016 3:17 am Post subject: |
|
|
Why can't you assign the variables for all templates and call
$smarty->display('extends: main.tpl|child1.tpl|child2.tpl' );?
Why must you parse the child templates separately?
It's technically not possible to use inheritance with separately parsed child templates. |
|
Back to top |
|
flexjoly Smarty Rookie
Joined: 08 May 2015 Posts: 15 Location: Apeldoorn, Netherlands
|
Posted: Mon Oct 10, 2016 11:13 am Post subject: |
|
|
Hi U.Tews,
Thanks for your reply.
Our pages are dynamicly and modulair build.
Modules like: menu, article, news-block, header, footer etc.
They cannot extends each other....
They have different variables. I use createTemplate():
$oTemplate = $oSmarty->createTemplate($tpl);
$oTemplate->assign($params);
Then:
$oTemplate->display();
or
$output = $oTemplate->fetch();
depending on its context.
Case:
The loaded article, set the title of the htmlpage.... this works very nice with extends... but is not working now
For now i made containerclasses which gather all information from all modules and then put it in the right place in the html.
I am not sure yet what works better (extends or these containers) |
|
Back to top |
|
|