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 {extends} file into extended file?

 
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
mustafaalhajji
Smarty n00b


Joined: 23 Feb 2021
Posts: 4

PostPosted: Tue Feb 23, 2021 6:15 pm    Post subject: how to {extends} file into extended file? Reply with quote

I'm a beginner at smarty and having difficulty working out a standard method to include frames and message boxes into my pages.

Here is the code I have and how I want it to function. I've been trying different methods for the last three days and ending nowhere:

index.php

Code:
$smarty->display('mypage.tpl')
;
mypage.tpl
Code:

{extends 'main_frame.tpl'}

{block 'title'}This is an example{/block}

{block 'body'}
  <p>This page is built with smarty</p>

  {extends 'message_box.tpl'}
  {block 'body'}
    <h1>Smarty is fun</h1>
  {/block}

{/block}

main_frame.tpl

Code:
<html>
  <head>
    <title>{block 'title'}{/block}{/title}
  </head>
  <body>{block 'body'}{/block}</body>
</html>

message_box.tpl
Code:

<div style='boarder: thin solid #f0f0f0;'>
  {block 'body'}{/block}
</div>

The rendered code should be something like this:
Code:

<html>
  <head>
    <title>This is an example{/title}
  </head>
  <body>
    <p>This page is built with smarty</p>
    <div style='boarder: thin solid #f0f0f0;'>
      <h1>Smarty is fun</h1>
    </div>
  </body>
</html>
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Feb 23, 2021 9:52 pm    Post subject: Reply with quote

1. No, you can not randomly disperse {extends} through the template. Only one is allowed, but you may list a chain of templates in PHP code. They will be extend'ing each other in order, effectively resulting in one template, again.
See https://www.smarty.net/docs/en/resources.extends.tpl

2. In extending template, only top-level elements allowed are {extends} {block}'s and comments.
No clear text, nothing else is allowed.

3. Another point is that you confuse formatting and content.

Thus, your solution is:

index.smarty:
Code:
<html>
  <head>
    <title>{block 'title'}Default title{/block}</title>
  </head>
  <body>{block 'body'}{block 'content'}No content.{/block}{/block}</body>
</html>


message_box.smarty:
Code:
{block 'body'}<div style='boarder: thin solid #f0f0f0;'>
  {block 'content'}Message without content.{/block}
</div>{/block}


my_message.smarty:
Code:
{extends 'message_box.smarty'}

{block 'title'}This is an example{/block}

{block 'body'}
  <p>This page is built with smarty</p>

  {block 'content'}{child}{/block}

{/block}
Back to top
View user's profile Send private message
mustafaalhajji
Smarty n00b


Joined: 23 Feb 2021
Posts: 4

PostPosted: Wed Feb 24, 2021 9:20 am    Post subject: Reply with quote

AnrDaemon,

Thank you for participating. I understand the work of extends but I was looking for a method to call templete into a template.

My example was very simple. The actual code is more decorative. The message box has title body and footer sections.
Sometime it will be used to only print a message while other times it may contain inputs and buttons

So to expand my example

Message box may looks like

Code:

<div class=messagebox>
<div class=header>
<h1>{block title} title {/block} </h1>
</div>
<div class=body>
{block body} {/block}
</div>
<div class footer>
{block footer} {/block}
</div>
</div>


Basically I want to include template message box to the page at specific position. It will be unreliable to do so with {function}.
Any clue how to approach it?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Feb 24, 2021 9:43 am    Post subject: Reply with quote

So, use {include}, not {extends}.
Back to top
View user's profile Send private message
mustafaalhajji
Smarty n00b


Joined: 23 Feb 2021
Posts: 4

PostPosted: Wed Feb 24, 2021 9:54 am    Post subject: Reply with quote

If I use include I cannot change the blocks of the included file (I tried)
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Feb 24, 2021 2:29 pm    Post subject: Reply with quote

Of course.

Again, you can extend only one template.

You can try using "extends:..." as a source of a template you extend (can't test ATM), but again, it will be a single resulting template you extend with your one.

Think again, and use right tools for what you want to achieve.
Back to top
View user's profile Send private message
mustafaalhajji
Smarty n00b


Joined: 23 Feb 2021
Posts: 4

PostPosted: Fri Feb 26, 2021 2:48 pm    Post subject: Reply with quote

That isn't the solution I hopped for.

I wanted a method where I can {include} template and change its {block}
Back to top
View user's profile Send private message
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
Page 1 of 1

 
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