What are templates and how do I create them?

Use the system templates, duplicate them or build your own Blade template with the placeholder toolbar. Plus import, export and troubleshooting.

Templates decide how your products look on your website: as a wide box, a grid, a table or a single price button. affiliate-toolkit ships with ready-made system templates, and you can create your own templates directly in the WordPress backend.

A template works like a mail merge: you write the HTML once and insert placeholders, and affiliate-toolkit fills in title, price, image and link of each product when the page is displayed. You manage all templates under affiliate-toolkit → Templates.

System templates

System templates come with the plugin and its modules, for example box, wide, secondwide, bestseller, grid_2_columns, grid_3_columns, datasheet or detailoffers. You use them in a shortcode by name: [atkp_product id="123" template="wide"][/atkp_product]. The overview shows a preview image for each one.

System templates cannot be edited, but every one has a Duplicate action. The copy becomes your own template, which you can change freely. That is the fastest way to start.

The templates overview filtered to the system templates, each with its shortcode and a Duplicate action
System templates can be duplicated with one click.

Create your own template

  1. Go to affiliate-toolkit → Templates and click Add New (or duplicate a system template).
  2. Enter a name and choose the Type. For product boxes and lists that is "Product template". Modules add more types: "Compare table" (Responsive Compare Table), "Search form template" (Frontend Search) and "Old template" (Template Compatibility).
  3. Write the markup in the editor. The toolbar above it inserts containers (default box, two or three columns), loops (product, product + offers, product + images) and every placeholder: product fields, offer fields, image fields, custom fields and, if ACF is active, ACF fields.
  4. Add your CSS in the CSS field below the editor and save.
  5. Use the template with its ID: [atkp_list id="456" template="789"][/atkp_list]. The overview shows the ready-made shortcode.
Template editor with Blade markup, a @foreach loop over $products, the placeholder toolbar above the editor and the CSS field
The editor with the placeholder toolbar.

A minimal example

Every product template needs a loop over $products. This template shows title, price and button of each product:

@foreach ($products as $product)
    <div class="my-product">
        <strong>{{ $formatter->get_title($product) }}</strong>
        {{ $formatter->get_pricetext($product, $translator->get_price()) }}
        <a {!! $formatter->get_button_link($product) !!}>{!! $formatter->get_button_text($product) !!}</a>
    </div>
@endforeach
Frontend output of a custom Blade template with image, title, star rating, price and buy button of the Kaffeewerk Barista Pro 3
A custom template on the website.

Technical background

Product templates use the BladeOne engine, a standalone version of the Blade engine known from Laravel. Blade gives you loops (@foreach), conditions (@if) and safe output ({{ }} escapes, {!! !!} outputs HTML). These variables are available in every template:

VariableContent
$productsArray of the products to render.
$shopsArray of shops, if a template renders shops instead of products.
$formatterHelper with all formatting methods (title, price, links, images …).
$translatorTranslated standard texts such as "Price: %s".
$parametersAll options from the shortcode, the template and the settings.
$shopThe shop of the first product.

The full list of methods is in Placeholders for Blade templates. For security, PHP tags, @php blocks and @inject are removed from templates created in the backend; PHP functions inside {{ }} expressions still work.

Provide your own variables

With the filter atkp_modify_bladedata you can pass your own values or objects to every template:

/**
 * Add your own variable to every Blade template.
 *
 * @param array $bladedata Variables passed to the template engine.
 * @return array
 */
function my_atkp_modify_bladedata( $bladedata ) {
    $bladedata['site_name'] = get_bloginfo( 'name' );
    // Available in the template as {{ $site_name }}

    return $bladedata;
}
add_filter( 'atkp_modify_bladedata', 'my_atkp_modify_bladedata' );

Import and export templates

To move a template to another website, export it and import it there.

  • Export: in the template overview, hover over your template and click Export. A .json file is downloaded.
  • Import: click Import template at the top of the overview, choose the file under "Template file" and confirm. Files with the extension .json and older .txt exports are accepted; ZIP files must be unpacked first.

Since version 3.8.6, raw PHP code (for example <?php ?> tags) is removed from imported templates and is not executed. Only import templates from sources you trust.

Troubleshooting

Templates are quick to build once you know the structure. Until then, these tips help:

Show error messages

By default, errors are hidden on the website. While you build a template, go to affiliate-toolkit → Settings → Display settings and untick Hide error messages on the web page. A box with the error message then appears instead of an empty output. Tick it again when you are done. Errors are also written to the affiliate-toolkit log.

Variable used outside its loop

The most common error is a placeholder that uses a variable which does not exist yet:

{{ $formatter->get_title($product) }}   <!-- error: $product does not exist yet -->

@foreach ($products as $product)
    {{ $formatter->get_title($product) }}   <!-- correct -->
@endforeach

Move the line into the @foreach loop and the error is gone. The same applies to $offer and $image, which only exist inside their own loops.

Undefined method get_mediumimage

The toolbar entry "Medium image" inserts get_mediumimage($product), but the method is called get_mediumnimage($product). Correct the name in your template.