Document table of contents

1. config_load function

Syntax:
(config_load file="file_name")

This function is used to load into the template variables from configuration files. In addition to the name of the file being loaded, this function may have several more additional options. For example, the section parameter, which specifies the name of the section to be loaded. More detailed information these and other options can be found in the Smarty documentation.

Example:
(config_load file="task.conf")

2.Capture function

Syntax:

(capture name="block_name"
assign="variable_name") ...
(/capture)

This function is intended to collect the output of the template into a variable instead of displaying it on the screen.

Anything between (capture name="varname") and (/capture) will be written to a variable named varname. The content captured in this way can be used in a template using the $smarty.capture.varname special variable, where varname is the value passed to the name attribute of the capture function. If no variable name is given, default will be used.

The second parameter assign specifies the name of the variable to which the captured output value will be assigned. This parameter, like name, is optional.

3. section function

Syntax:

(section name="section_name"
loop="variable_to_calculate_the_number_of_iterations"
[,start="start_position_index"]
[, step="step"] [,max="max_iterations"]
[,show="show_section"] )...
(/ section)

The Section is a loop for traversing the elements of an array. Mandatory parameters are name, which specifies the name of the section, and loop, which is a variable that determines the number of iterations of the loop.

Typically, loop is an array variable, and the number of iterations of a section is equal to the number of elements in that array. To display a variable inside a loop, you need to specify the section name in square brackets after the variable name.

(section name=art loop=$title)

Title: ($title)

(/ section)

Example 15.8. Loop to iterate over array elements

4. foreach function

Syntax:

(foreach from="array_name"
item="current_item_name")
... (/foreach)

In addition, you can use additional attributes key - the name of the key for the current element of the array and name - the name of the loop, with which you can access its properties. The from and item attributes are required.

foreach loops are an alternative to section loops. The foreach function is very similar to the foreach loop in PHP.
(foreach from=$articles item=art)
Title: ($art)

(/foreach)

Example 15.9. foreach loop

foreach loops have their own properties. They can be accessed like this: ($smarty.foreach.foreachname.varname), where foreachname is the name of the loop given by its name parameter, and varname is the name of the property.

5. If, elseif, else statement

Syntax:

(if expression) action_block
(elseif expression1) action_block1
(else) action_block2
(/if)

The action of the operator is almost similar to the if...elseif...else operator of the PHP language.

The following comparison operators can be used in expressions: eq, ne, neq, gt, lt, lte, le, gte, ge, is even, is odd, is not even, is not odd, not, mod, div by, even by, odd by, ==, !=, >,<, <=, >=. Each of them must be separated from the surrounding values ​​by spaces. Parentheses can be used in expressions and PHP functions can be called.

(if $name eq "Vasya")
Welcome Vasya.
(elseif $name eq "Petya")
Welcome, Petya.
(else)
Welcome. And who are you?
(/if)

Example 15.10. if, elseif, else statements

(* this example will not work because there are no spaces around the comparison operators *)
(if $name=="Vasya" || $name=="Peter")
...
(/if)
Example 15.11. Broken Example

When creating sites on self-written (without the use of frameworks, CMS, and other fashionable things that make life easier for web developers), we are faced with the problem of making changes to the site when there are a lot of pages.

So that we do not have to change the same parts of the site in each of the page files, we can use convenient PHP instructions that allow us to include files with the necessary code in all pages with just one line of code. Then, by changing the content of the included file, we change the code on all pages of the site. Convenient no matter how you look at it.

Now let's look at ways to connect files in more detail:

Using include and require

You will not be able to find a fundamental difference between these two instructions with all your desire, but there are nuances:

If an error occurs during the execution of the require command, the parser will receive a fatal error response and the execution of the page code will stop, while include will only issue a warning and the file will continue to run (the file will simply not be included).

Let's take a simple example to better understand the topic.

We have our mini-site in which the header and footer are the same on all pages, but the body of the document changes.

We create the header.php and footer.php files in which we place the code that will be the same on all pages, and in the index.php and newpage.php files we will connect the static parts. As a result, we get:

header.php content

< header> < nav> < a href= "newpage1.php" title= "menu item" >menu item < a href= "newpage2.php" title= "menu item" >menu item < a href= "newpage3.php" title= "menu item" >menu item

footer.php content

< footer> < p>Made by us

Content of other pages of the site

Minisite

Lots of useful information

As a result of loading our page, we get the following picture:

As we can see, everything works great.

Please note that we ran the example on local server Denwer, because for PHP work you need a server with its support. If you create a site in a simple folder on a PC, nothing will work.

In the considered example, we used the require header to connect, and the include footer. What to use on your sites is up to you. As already mentioned, there is not much difference between them. Unless require is considered a somewhat stricter instruction.

In fact the line simply copies the entire contents of the file, the path to which we specify, inside the document in which it is located.

Using include _once and require _once

When working on the site, problems may arise due to the fact that the same piece of code is included in one file more than once.

Suppose this happened due to the fact that several people worked on the site, and when the codes were combined, then such an incident came out ...

To eliminate the possibility of such problems, developers often use the include _once and require _once statements. Their principle of operation is exactly the same as that of include with require, but if the file in such an instruction has already been connected to ours, then reconnect will not happen.

To disadvantages this method can be attributed to the fact that it is slower and more computationally expensive than its predecessors include with require, since it becomes necessary to remember all included files and compare them to avoid code duplication.

On a note

  • To make it more convenient to distinguish between the site page files and the files of their fragments, which we include in them, usually included files add an inc part to the name. Considering this approach in our example, we would get the header.inc.php file from the header.php file, and so on. This approach can greatly simplify the understanding of the structure of the site in the future.
  • The code from the file that we include inherits the scope of the line in which it is included. Tobish just inside the page it will have a global scope, and inside the function it will have a local scope.
  • We can use include with require anywhere we want. Even inside scripts.

). Each tag (section) must have a couple (/ section). The required parameters are name and loop. The cycle name (section) can be anything, consisting of letters, numbers and underscores. Cycles (section) can be nested and nested (section) names must be unique among themselves. Variable loop(usually an array of values) specifies the number of iterations of the loop. When printing variables inside a section, the section name must appear next to the variable name inside square brackets. (sectionelse) executed if the parameter loop contains no values.

Attribute name Type of Mandatory Default Description
name string Yes n/a Section name
loop mixed Yes n/a A value that specifies the number of iterations of the loop.
start integer Not 0 Index of the position from which the loop will start. If the value is negative, then the starting position is calculated from the end of the array. For example, if the loop variable has 7 elements and the value of the start attribute is -2, then the start index will be 5. Invalid values ​​(values ​​outside the array) are automatically truncated to the nearest valid value.
step integer Not 1 The step value that is used to iterate through the array. For example, step=2 specifies to traverse the array by elements 0,2,4... If the step is negative, then the array will be traversed in the opposite direction.
max integer Not 1 The maximum number of loop iterations.
show boolean Not true Specifies whether or not to show this section

note

Since Smarty 1.5.0, the session property variable syntax has changed from (%sectionname.varname%) to ($smarty.section.sectionname.varname). The old syntax is still supported, but you will only see examples of the new syntax.

index is used to display the current index of the array, starting at zero (or the start attribute, if one was specified) and incrementing by one (or the value of the step attribute, if one was specified).

Technical Note

If the step and start attributes are not specified, then index is the same as the iteration section attribute, except that it starts at 0 instead of 1.

iteration is used to display the current iteration number of the loop.

note

This value is independent of the start, step, and max properties, unlike the index property. Also, iterations start at one, not zero like indexes do. rownum is a synonym for the iteration property, they work in the same way.

Example 7.38. property (section) iteration

assign("custid",$id); ?> (section name=cu loop=$custid start=5 step=2) iteration=($smarty.section.cu.iteration) index=($smarty.section.cu.index) id=($custid)
(/ section)

The result of running this example:

Iteration=1 index=5 id=3005
iteration=2 index=7 id=3007
iteration=3 index=9 id=3009
iteration=4 index=11 id=3011
iteration=5 index=13 id=3013
iteration=6 index=15 id=3015

This example uses the iteration property to display the table header every five lines (uses (if) with a mod operator - modulo).

(section name=co loop=$contacts) (if $smarty.section.co.iteration % 5 == 1) (/if) (/ section)
Name>HomecellEmail
view ($contacts.name) ($contacts.home) ($contacts.cell) ($contacts.email)


An article that considers the section HTML element from the sectioning category.

Purpose of the section element

The section element is used to create a section in a document that groups some subject content together. For each section in the document, its title (subject) should be indicated. This is usually done with headings (h1 - h6 elements).

Section heading

Section content...

section elements are typically used in the following cases:

  • to mark up sections within a section. For example, to mark up chapters in an article, tabs in a dialog box, sections in a dissertation, etc.
  • to group several sections into one thematic group. For example, to group latest news on the site, comments on the article, etc.

Thus, the section element should only be used for some content if it has a heading and is part of something else.

Applying the section element

For example, consider a page code snippet containing an article with comments. Each of the comments left by the user on the page contains some completed content and can therefore be considered an article element. But, at the same time, all comments represent some topic group, and therefore they can be placed in the section element, i.e. this element will group all those comments on the page together.

Article title

Comments

Comment title

Comment text...

Comment title

Comment text...

Title of the article Comments Title of the comment Title of the comment

For example, consider using section elements to create sections within an article element:

Name of the book

First chapter

Second chapter

Third chapter

Annex A

Annex B

The above example would have the following structure (outline):

Book Title First Chapter Second Chapter Third Chapter Appendix A Appendix B

Restrictions when using the section element

The section element in HTML 5 is not a generic element for grouping content; it should not be used to wrap any content you like. Its main purpose is to add semantics to the document and create its structure (outline).

When an author needs to group content just to style it or work with it in JavaScript, the best thing to do is use the div element. The div element, unlike the section element, does not add semantics to the document and does not participate in the creation of its structure (outline).

Difference between section and article elements

The section and article elements, while seemingly very similar, have different semantic meanings. The article element is for grouping content that is complete, standalone, and can be considered separate from the rest of the page's content. And the section element has a different semantic meaning, it is intended for grouping content, which is integral part something else.

But how does an author know what some content on a page is? Let's look at this using an example article fragment. A fragment is part of an article and, therefore, the section element must be used to group its content. But the same fragment, already left as a commentary, will be something whole, complete. Therefore, in this context, the article element can be used to group it. But, of course, the opposite can also be argued. Therefore, which element to use to group content in most cases depends on your subjective opinion as an author. But the most important thing in this approach is to maintain the chosen position. Therefore, the more consistent the author is in creating the structure, the more sense he will be able to put into it.