View previous topic :: View next topic |
Author |
Message |
whofarted75 Smarty Rookie
Joined: 15 Sep 2020 Posts: 6
|
Posted: Tue Feb 16, 2021 11:32 pm Post subject: What is the diff. between {include} & {extends}? |
|
|
I read the documentation but for me it was a little confusing. What is the difference between the two It seems to be pretty much the same thing. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Feb 17, 2021 8:16 am Post subject: |
|
|
They are the opposite of one another.
For details and usage examples, see the docs. |
|
Back to top |
|
whofarted75 Smarty Rookie
Joined: 15 Sep 2020 Posts: 6
|
Posted: Thu Feb 18, 2021 2:04 am Post subject: |
|
|
AnrDaemon wrote: | They are the opposite of one another.
For details and usage examples, see the docs. |
um....
whofarted75 wrote: | I read the documentation |
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Feb 19, 2021 8:04 am Post subject: |
|
|
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 |
|
whofarted75 Smarty Rookie
Joined: 15 Sep 2020 Posts: 6
|
Posted: Sun Feb 21, 2021 7:15 pm Post subject: |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sun Feb 21, 2021 11:28 pm Post subject: |
|
|
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 |
|
whofarted75 Smarty Rookie
Joined: 15 Sep 2020 Posts: 6
|
Posted: Mon Feb 22, 2021 6:14 pm Post subject: |
|
|
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.
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. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Feb 23, 2021 5:32 pm Post subject: |
|
|
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 |
|
|