View previous topic :: View next topic |
Author |
Message |
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Apr 06, 2015 5:34 am Post subject: Allow declaring "abstract" templates |
|
|
This is a wishful thinking, but here's a use case.
I have a main page template (page.tpl):
Code: | <html><head>
<title>{block 'page-title'}Silly title{/block}</title>
</head><body>{block 'page-content'}Silly content{/block}</body></html> |
An intermediate template (list-index.tpl):
Code: | {extends 'index.tpl'}
{block 'page-content'}
{block 'pager'}[1] [2] [3]{/block}
{block 'list-content'}x{/block}
{block 'pager'}[1] [2] [3]{/block}
{/block} |
And two child templates:
content.tpl: Code: | {extends 'index.tpl'}
{block 'page-title'}{$title|escape}{/block}
{block 'page-content'}{$content|render}{/block} |
list-content.tpl: Code: | {extends 'list-index.tpl'}
{block 'page-title'}{$title|escape}{/block}
{block 'pager'}{somepaging}{/block}
{block 'list-content'}{somecontent}{/block} |
This is all fine and dandy until someone try to render index.tpl or list-index.tpl directly. Yes, amusing text that pops up subtly tells that you shouldn't be doing it, but what I'd really like to see is a SmartyCompilerException telling to never do this mistake again.
This could be implemented through additional flag "abstract" added to the block.
Blocks declared as abstract MUST have no content.
Direct attempt to render (compile) the template with abstract block declared MUST result in SmartyCompilerException.
If this functionality is implemented somehow already, can you please point me to it? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Apr 06, 2015 2:53 pm Post subject: |
|
|
Currently there is no solution.
But I like the 'abstract' idea.
But if you have more then two inheritance levels it might still not ensure that started with the correct outer level child template. A block might be defined in the child, but overloaded in the grandchild. But it could be done with blocks only defined in the outer level. (maybe by an empty one just for this purpose)
In some future release inheritance be no longer a compile time process (for new features and more flexibility). But it could be a run time check.
I will put it on the TODO list. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Apr 06, 2015 6:16 pm Post subject: |
|
|
Thank you! As long as there's the functionality present in some way, I'll find a way to use it
Takes a careful planning of your inheritance model, but it pay back well in the end. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Apr 08, 2015 11:58 pm Post subject: |
|
|
That can get a bit tricky, you probably don't want to mark a block as abstract if it is already extending a block from a parent template. Maybe it would make more sense to mark templates as abstract instead of blocks. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Apr 09, 2015 1:43 am Post subject: |
|
|
I've thought about it, but I don't see how it can be implemented, short of adding new entities, not currently present in the design.
And regarding specifically marking block abstract, that is extending another block, consider the abovementioned use case.
I may use index.tpl directly with minimal modifications, but I need to prevent intermediate list-index.tpl from being used directly, because its main purpose is to duplicate the same navigation bar at the top and bottom of the page.
Last edited by AnrDaemon on Thu Jan 11, 2018 6:31 am; edited 1 time in total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Apr 09, 2015 2:46 am Post subject: |
|
|
It may be the case that the utility of an abstract template supersedes its complexity. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Apr 09, 2015 9:34 pm Post subject: |
|
|
I still like the abstract block attribute to detect missing block definitions expected to be in child templates. |
|
Back to top |
|
|