Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How to use extends with multiple subtemplates?
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
flexjoly
Smarty Rookie


Joined: 08 May 2015
Posts: 15
Location: Apeldoorn, Netherlands

PostPosted: Fri Sep 09, 2016 10:09 pm    Post subject: How to use extends with multiple subtemplates? Reply with quote

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
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Sep 10, 2016 6:51 am    Post subject: Reply with quote

Can't it be both?
Extension is not a replacement for inclusion.
Back to top
View user's profile Send private message
flexjoly
Smarty Rookie


Joined: 08 May 2015
Posts: 15
Location: Apeldoorn, Netherlands

PostPosted: Sat Sep 10, 2016 9:09 am    Post subject: Reply with quote

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) Sad
Back to top
View user's profile Send private message Visit poster's website
flexjoly
Smarty Rookie


Joined: 08 May 2015
Posts: 15
Location: Apeldoorn, Netherlands

PostPosted: Sat Sep 10, 2016 11:11 am    Post subject: Reply with quote

I found 'extendsall' : https://github.com/smarty-php/smarty/issues/67

Not working yet, but looking into it.
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Sep 10, 2016 12:24 pm    Post subject: Reply with quote

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) Sad

Fix your broken architecture. And the answer will be a simple $smarty->display().
Back to top
View user's profile Send private message
flexjoly
Smarty Rookie


Joined: 08 May 2015
Posts: 15
Location: Apeldoorn, Netherlands

PostPosted: Sat Sep 10, 2016 3:29 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Sep 10, 2016 4:19 pm    Post subject: Reply with quote

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
View user's profile Send private message
flexjoly
Smarty Rookie


Joined: 08 May 2015
Posts: 15
Location: Apeldoorn, Netherlands

PostPosted: Sat Sep 10, 2016 6:02 pm    Post subject: Reply with quote

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 Sad

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 Laughing Embarassed

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
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Sep 10, 2016 7:56 pm    Post subject: Reply with quote

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
View user's profile Send private message
flexjoly
Smarty Rookie


Joined: 08 May 2015
Posts: 15
Location: Apeldoorn, Netherlands

PostPosted: Sat Sep 10, 2016 10:18 pm    Post subject: Reply with quote

Lets agree to disagree. Wink

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 Wink
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 Twisted Evil

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
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Sep 10, 2016 10:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
flexjoly
Smarty Rookie


Joined: 08 May 2015
Posts: 15
Location: Apeldoorn, Netherlands

PostPosted: Sun Sep 11, 2016 12:08 pm    Post subject: Reply with quote

I do agree on singleton being an anti-pattern, but it is very usefull.... although i do hate it. Wink

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 Wink (just to remember what the real topic is) Wink
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Sep 11, 2016 8:27 pm    Post subject: Reply with quote

Nowhere I said that OOP is not applicable.

If your black box renders itself, then Smarty::registerPlugin() its rendering method.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Sep 15, 2016 3:17 am    Post subject: Reply with quote

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
View user's profile Send private message
flexjoly
Smarty Rookie


Joined: 08 May 2015
Posts: 15
Location: Apeldoorn, Netherlands

PostPosted: Mon Oct 10, 2016 11:13 am    Post subject: Reply with quote

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 Sad

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) Rolling Eyes
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP