Child pages
  • Szablony tematyczne i Smarty

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of contentsSpis treści

Table of Contents

...

Szablony tematyczne i Smarty

Smarty is a PHP template engine that is sturdy, fast, easy to learn and use, and has a clean and compact design-dedicated syntax. It helps building a much simpler HTML code, and works by compiling then caching each page.You can learn more by going to its official websitejest silnikiem szablonu PHP który jest wytrzymały,szybki, łatwy w nauce i użyciu, i ma czystą i kompaktową składnie do projektowania dedykowanego. Pomaga zbudować znacznie prostszy kod HTML i działa poprzez gromadzenie, a następnie buforowania każdej strony.

Możesz się dowiedzieć więcej wchodząc na oficjalną stronę: http://www.smarty.net/

...

Podstawowe zasady

Delimiters

...

Ograniczniki

Ograniczniki umozliwiają silnikowi szablonu "rozpoznać" Smarty zwraca się wewnątrz szablonu. Ograniczniki wykorzystywane przez Smarty są w nawiasach klamrowych The delimiters used by Smarty are curly braces: {smarty_call}

Note

If you need to use actual curly braces within your template code, Smarty has special calls for youJeśli trzeba użyć rzeczywistych nawiasów klamrowych w kodzie szablonu Smarty posiada specjalne połączenie dla Ciebie: {redelim} for a left curly brace na lewej nawias klamrowy ({), and i {rdelim} for a right curly brace dla prawej nawias klamrowy (}).

Comments

As with any scripting or programming language, you can put comments within your template code, and the template engine will not parse them.

Comments use regular delimiters, along with opening and closing * characters:

Komentarze

Jak w przypadku każdego języka programowania lub skryptów, można umieścić komentarze w kodzie szablonu, a silnik szablonu nie będzie ich analizowac. 

Komentarze używają regularnych ograniczników, wraz z otwieraniem i zamykaniem * znaków.

 

Code Block
{* One-line comment. *}

{* 
Multiple
lines
comment.
*}

{* Comment for Smarty {$code} *}

Variables

Just as in PHP, variable are represented by a dollar sign followed by the name of the variable. Putting a variable directly within a Smarty delimiter means that you want to display the variable as-is.
For instance, {$foo} is the Smarty equivalent of PHP's echo $foo;.

...

Zmienne

Tak jak w PHP, zmienne są reprezentowane przez znak dolara, po którym następuje nazwa zmiennej. Umieszczenie zmiennej bezpośrednio w ograniczniku Smarty oznacza, że chcesz wyświetlić zmienną taką jaka jest.
Na przykład, {$foo} Smarty jest odpowiednikiem PHP echo $foo;.

Można również stosować moyfikatory do zmiennych:

Code Block
{* Displaying the content of the variable in lowercase. *}
{$foo|lower}
    
{* Displaying the content of the variable after replacing one word with another. *}
{$foo|replace:'bar':'baz'}
    
{* Example of "chaining" modifiers.	 *}
{$foo|lower|capitalize|truncate:10:'...'}

You can get a complete list of the available modifiers on the official websiteMożesz uzyskać pełną listę dostępnych modyfikatorów na oficjalnej stronie internetowej: http://www.smarty.net/docs/en/language.modifiers.tpl

...

Tryby warunkowe

Smarty has a conditional ma warunkowy system if / else / elseif system :

Code Block
{if $coffee == 'good'}
  {* Happy coder *}
{elseif $coffee == 'very good'}
  {* Very happy coder *}
{else}
  {* Grumpy coder *}
{/if}

You can learn more about the various conditionals on the official websiteMożesz się dowiedzieć więcej o różnych trybach warunkowych na oficjalnej stronie: http://www.smarty.net/docsv2/en/language.function.if.tpl

...

Pętle

Smarty supports two loopsobsługuje dwie pętle: section and i foreach. You can use iterators, and even the foreachelse conditionalMożna użyć iteratorów, a nawet trybu warunkowego foreachelse :

Code Block
<?php
  $items_list = array( 23 => array('no' => 2456, 'label' => 'Salad'), 96 => array('no' => 4889, 'label' => 'Cream') );
  $smarty->assign('items', $items_list);
?>
<ul>
{foreach from=$items key=myId item=i}
  <li><a href="item.php?id={$myId}">{$i.no}: {$i.label}</li>
{/foreach}
</ul>

You can learn more on each loop on their respective pages on the official websiteMożesz dowiedzieć się więcej o każdej pętli na oficjalnych stronach internetowych:

File inclusion

...

Włączanie pliku

Można łatwo dołączyć plik szablonu do innego używając funkcji include, extends or block functions. Thanks to inheritance, file inclusion can impact many templates at a time.

The assignation method takes two mandatory arguments:

  • file: the name of the template file to include.
  • assign: the name of the variable that the output of include will be assigned to.

Here are a couple examplesDzięki dziedziczeniu, włączenie pliku może mieć wpływ na wiele szablonów naraz.

Metoda przypisania używa dwóch obowiązkowych argumentów:

  • file: Uwględnia nazwę plikiu szablonu.
  • assign: Nazwa zmiennej wyjścia zostanie przypisana.

Oto kilka przykładów:

Code Block
{* Including a file. *}
{include file='steps.tpl'}
   
{* Placing the included content in a variable, then display the variable.	 *}
{include file='text.tpl' assign='MyText'}
{$MyText}

Demonstration of inheritance with extends and Demonstracja dziedziczenia z  extendsblock:

Code Block
{* Filename: parent.tpl *}
<html>
<head>
<title>{block name="title"}Default Title{/block}</title>
...
{block name="title"}New Title{/block}
</head>
</html>

{* Filename: child.tpl *}
{extends file='parent.tpl'}
{block name='title'}New Page Title{/block}

That last example uses the block function, which is designed to define a named area of template source for template inheritance.

You can learn more about template inheritance and each inclusion function on their respective pages on the official websiteTen ostatni przykład używa funkcji  block, który jest przeznaczony do określenia nazwy powierzchni żródła matrycy do matrycy dziedziczenia.

Można się dowiedzieć wiecej na temat dziedziczenia szablonów i każdej funkcji umieszczenia na swojej stronie na oficjalnej stronie:

Debug function

The complete set of internal processes from a Smarty template can be displayed when the page is displayed.

...

Funkcja debugowania

Kompletny zestaw procesów wewnętrznych z szablonu Smarty mogą być wyświetlane, gdy wyświetlana jest strona.

Podczas rozwoju tematycznego, można to zrobić dla każdego ładowania strony poprzez edycje pliku /config/smarty.config.inc.php file and editing the i edycje $smarty->debugging value wartości:

Code Block
$smarty->debugging = true;

Once the theme is on a production site, you can enable the debug function by adding the {DEBUG} directive in the template file.

You can also manage the debug function directly from PrestaShop: in the "Advanced Parameters" menu, in the "Maintenance" page, change the "Debug console" option to your liking.

You can learn more about the debug function on the official websiteGdy motyw jest na stronie produkcyjnej, można włączyć funkcje debugowania poprzez dodanie dyrektywy{DEBUG} w pliku szablonu.

Można także zarządzać funkcją debugowania bezpośrednio z PrestaShop w menu "Paramenty Zaawansowane" na sronie "Konserwacja" zmień opcje "Konsola Debugownia" do swoich upodobań,

Możesz dowiedzieć się więcej o tej funkcji debug na oficjalnej stronie: http://www.smarty.net/docs/en/language.function.debug.tpl

...

Zaawansowane zastosowania

...

Zrzuty

The capture function makes it possible to retrieve the output of a template without displaying it. For instanceFunkcja capture umożliwia pobieranie danych wyjściowych szablonu bez wyświetlania go. Na przykład: {capture name="myCapture'} ... {/capture}

In order to use the content, you must call the $smarty super-variableW celu korzystania z takich materiałów należy przywołać $smarty super zmienna: $smarty.capture.myCaptureDo not forget to test for the capture's existence before using it

Nie zapomnij sprawdzić chwytu przed użyciem go:

Info

{if $smarty.capture.<name>} or {if isset($smarty.capture.<name>}

The capture function is also able to auto-assign a variableFunkcja przechwytywania ma możliwość automatycznego przypisania zmiennej:

Code Block
{capture name='myCapture' assign='myVar'}
   ...
{/capture}
{* Then, in order to use the content, call the $myVar variable. *}

You can learn more about the capture function on the official websiteMożesz dowiedzieć się więcej o funkcji capture na oficjalnej stronie: http://www.smarty.net/docs/en/language.function.capture.tpl

Assigning variables

...

Przypisanie zmiennych

Możliwe jest przypisanie zmiennej do pliku szablonu (widok) używając funkcję  assign :

Code Block
{assign var='myVar2' value='My value'}

{* Will display "My value". *}
{$myVar2}

You can learn more about the assign function on the official websiteMożesz dowiedzieć się więcej o funkcji assign na oficjalnej stronie: http://www.smarty.net/docs/en/language.function.assign.tpl

...

Zmienna $smarty

...

$smarty is a so-called super-variable. It makes it possible to retrieve some useful information jest to tak zwana super zmienna. To sprawia, że można pobrać kilka przydatnych informacji:

  • capture values wartości: $smarty.capture.myVariable
  • wartości GET values: {$smarty.get.<name>}
  • wartości POST values: {$smarty.post.<name>}
  • Current aktualny timestamp: {$smarty.now}, or with customized formatting lub niestandardowego formatowania {$smarty.now|date_format:'%d-%m-%Y %H:%M:%S'}
  • stałe PHP constants: {$smarty.const.<constant name>}
Note

In w Smarty 2, $smarty could be used with mogą być stosowane foreach:

When the loop is defined asGdy pętla jest zdefiniowana jako: {foreach from=$myarray key="mykey" item="myitem"}
...you can perform a można wykonać wezwanie $smarty.foreach.name.property call

Since Od PrestaShop 1.5 , it is recommended to only rely on the Smarty 3 syntax from PrestaShop themes. Therefore, a zaleca się polegać tylko na składni Smarty 3 z szablonu PrestaShop, W związku z tym $smarty.foreach.varName.property call must be replaced by the $varName@property equivalent call.

...

wezwanie call musi być zastapiony przez $varName@property równoważne wezwanie.

Możesz dowiedzieć więcej o zmiennej $smarty na oficjalnej stronie: http://www.smarty.net/docs/en/language.variables.smarty.tpl

...

Literówki

The literal tag makes it possible for a data block to be used as-is, literally, without Smarty trying to interpret itEtykieta literal umożliwia blokowi danych do wykorzystanie jej dosłownie, bez Smarty próbuje je interpretować:

Code Block
{literal}
<script type="text/javascript">
  function myFonction()
  {
    ...
  }
</script>
{/literal}

You can learn more about the literal function on the official websiteMożesz dowiedzieć się więcej na temat funkcji dosłownej na oficjalnej stronie: http://www.smarty.net/docs/en/language.function.literal.tpl

...

Funkcje

Smarty functions do not use the Funkcje Smarty nie używa $ prefix, as do variablespodobnie jak zmienne: {debug}, {rdelim}, {ldelim}, ...

They can accept argumentsMogą one zaakceptować argumenty: {include file='<name of the file>'}

The structure of a function call is thusStruktura wywołania funkcji jest więc: {nameOfTheFunction arg1='...' arg2='...'}

You cannot use modifiers on functions. For instance, Nie można użyć modyfikatorów na funkcjach, na przykład {nameOfTheFunction arg1='...' |lower} will not work as expected.You can learn more about Smarty functions on the official website nie będzie działać zgodnie z oczekiwaniami. .

Możesz dowiedzieć sie więcej o funkcjach Smarty na oficjalnej stronie:

...

Wtyczki

Smarty plugins makes it possible to easily extend the standard behavior. They are written in PHP.

For instance, PrestaShop has a Smarty plugin that creates a specific function to handle translatable stringsWtyczki Smarty umożliwia łatwe rozszerzenie standardowego zachowania. Są one zapisane w PHP. Na przykład, PrestaShop ma wtyczki Smarty, które tworzą specyficzną funkcje obsługiwania tłumaczeń: {l}First, in a theme

Po pierwsze w temacie:

Code Block
{l s='Hello dear viewer'}

i w module (nawet jeśli jest nadpisany!) And in a module (even if overridden!):

Code Block
{l s='Hello dear view' mod='myModule'}

You can learn more about Smarty plugins on the official websiteMożesz dowiedzieć się więcej o wtyczkach Smarty na oficjalnej stronie: http://www.smarty.net/docs/en/plugins.tpl

Smarty

...

A few things that you should expressly avoid when using Smarty:

...

niemile widziane z PrestaShop

Kilka rzeczy które trzeba wyraźnie unikać przy użyciu Smarty:

  • Nie rób stałego bezpośredniego połączenia PrestaShop. Bardziej szczegółowo nawet nie używaj {$smarty.const._DB_PASSWD_}, for obvious reasons.
  • Do not override PrestaShop's assigned variables.
  • Do not make the code needlessly hard to read. For instance, refrain from making include calls from within an already included file.
  • Do not make direct PHP calls. For instance, do not use z oczywistych powodów..
  • Nie zastępuj przypisanych zmiennych PrestaShop.
  • Nie twórz niepotrzebnie kodu trudnego do odczytania. Na przykład powstrzymując się od jakichkolwiek zawołań  include od dołączonego już od wewnątrz pliku.
  • Nie rób bezpośredniego połączenia PHP. Na przykład nie należy używać {php} // PHP code {/php}