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

What is the diff. between {include} & {extends}?

 
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
whofarted75
Smarty Rookie


Joined: 15 Sep 2020
Posts: 6

PostPosted: Tue Feb 16, 2021 11:32 pm    Post subject: What is the diff. between {include} & {extends}? Reply with quote

I read the documentation but for me it was a little confusing. What is the difference between the two Question It seems to be pretty much the same thing.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Feb 17, 2021 8:16 am    Post subject: Reply with quote

They are the opposite of one another.

For details and usage examples, see the docs.
Back to top
View user's profile Send private message
whofarted75
Smarty Rookie


Joined: 15 Sep 2020
Posts: 6

PostPosted: Thu Feb 18, 2021 2:04 am    Post subject: Reply with quote

AnrDaemon wrote:
They are the opposite of one another.

For details and usage examples, see the docs.

um....

whofarted75 wrote:
I read the documentation

Confused
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Feb 19, 2021 8:04 am    Post subject: Reply with quote

No, you did not.
Else you would notice that include - includes content of the referenced template to the referencing one; extends - extends content of the referenced template with blocks from referencing one.
Back to top
View user's profile Send private message
whofarted75
Smarty Rookie


Joined: 15 Sep 2020
Posts: 6

PostPosted: Sun Feb 21, 2021 7:15 pm    Post subject: Reply with quote

AnrDaemon wrote:
No, you did not.
Else you would notice that include - includes content of the referenced template to the referencing one; extends - extends content of the referenced template with blocks from referencing one.

Well first of all, you don't know what I have and have not read. So I guess we'll just drop that one right now.

Secondly it's exactly as I said, "there doesn't appear to be that much difference." So the question is what is the difference?

Because to me if you have one template that is included you could assign a variable to the template and print it out, whereas if you're using extends, it's the exact same thing only instead of it's assigning a variable in PHP to the template you simply put where you wanted to appear by using the keyword block. So essentially they do the exact same thing.

If you're not going to help please just ignore this question and let someone else explain it that and what the difference is are.

If the only difference is or what has been explained here, and in the manual, then I would ask you what's the difference between this:
Code:

.php
$smarty->assign( 'SOMETHING', $something );
.html (or .tpl)
<div>{$SOMETHING}</div>

VS:
Code:

.php
$smarty->assign( 'SOMETHING', $something );
.html (or .tpl)
{extends file="layout.html"}
{block name="thisBlock"}
<div>{$SOMETHING}</div>
{/block}

Because the second way just looks like a more difficult way to do the first way.

That's why I asked the question. I don't see the point of how it's more useful to use the blocks. I assume I'm missing something as far as functionality otherwise why would anyone take the time to do this?
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sun Feb 21, 2021 11:28 pm    Post subject: Reply with quote

I see the difference as being the "point-of-view".

Given templates A and B. A is a superstructure (<html>,<head>,<body>, etc.). B is a component (<table>, etc).

include: you are in A wanting to absorb and incorporate any one of several possible B.

extend: you are in B wanting to populate corresponding named blocks to be found in any one of several possible A.

I suppose for much the same reason why PHP has both include() -- "if this, then include(that)"; and extend (OOP classes) -- "I am mysqli, but I need a database abstraction parent class".

So, different points of view, each solving (maybe) the same problem with efficiencies and variable arrangements uniquely possible to that view.
Back to top
View user's profile Send private message
whofarted75
Smarty Rookie


Joined: 15 Sep 2020
Posts: 6

PostPosted: Mon Feb 22, 2021 6:14 pm    Post subject: Reply with quote

Thanks bsmither,

That's mostly what I was wondering is if it does something completely different or it's just a different way of doing something. Kind of like you can write an entire website using procedural PHP or you can write it entire website using OOP, both of them work & both of them accomplish the same thing they just have a different way of going about it. And one may be more manageable than another in a large project.

I was just unsure if there was some kind of a functionality that I was missing or if I missed the point of blocks completely. Wink

for example:

Code:
{extends file="parent.tpl"}
{block name="title"}
Page Title
{/block}

could be done just as easily as:

Code:
.php
$smarty->assign( 'TITLE', $title );
.html (or .tpl)
<title>{$TITLE}</title>

See, by the examples they gave it seemed completely unnecessary.

I use this to inject extra header information into my templates:
Code:
header.php

//
// Added header info (in case you need javascript in the header area)
// If blank it will cause errors in the template So they MUST be set
// we'll set them here & inherit setting before including header in other php pages
//
$headerTagExtras    = (!empty($headerTagExtras))    ? $headerTagExtras    : '';
$bodyTagExtras       = (!empty($bodyTagExtras))       ? $bodyTagExtras    : '';

$smarty->assign( 'HEADERTAGEXTRAS', $headerTagExtras );
$smarty->assign( 'BODYTAGEXTRAS', $bodyTagExtras );

header.html

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>My Title</title>

  <link rel="stylesheet" href="templates/{$SITE_STYLE}/css/style.css">

  <script src="includes/jquery-3.5.1.min.js"></script>
  {$HEADERTAGEXTRAS}

</head>
<body {$BODYTAGEXTRAS}>

By doing that with the header I can add JavaScript into the header or body tag or whatever I need to do including a countdown to refresh the page or whatever.

That's why I was asking this question because it looked like I could add a block to the <head> but it seems to do the same thing so it I was trying to decide should I spend the time redoing my templates for something like that.

That's all. Thanks. Laughing
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Feb 23, 2021 5:32 pm    Post subject: Reply with quote

No, it can not be "done as easy".
Including dynamic files require additional overhead of passing all the names and additional checking of them passed at the include point.
Extending removes such overhead entirely and even offer additional features. My often cited example being pagination row rendered once but displayed twice in the parent template.
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