Skip to content

templateFunctions

Kim Vinberg edited this page Aug 12, 2023 · 8 revisions

What is templateFunctions Template function are a system desgined to only load the code that is needed. Its quite simple. After SEO is loaded and we have checked for a cached version of the current page then we start to load a fresh one by including the templateFunctions.php file.

First we find out what template files to load (from the seo.php file). All parts load head.tpl , header.tpl and footer.tpl but then we load more templates based on the current URL.

Example the homepage loads index.tpl (row in the database) tell us this.

Inside the index.tpl we can then find a section called featured products.

			<div class="col-md-12"><h2>Featured products</h2></div>
				{section name=product loop=$displayRandomProducts}
				<div class="col-md-3">
					<div href="{$displayRandomProducts[product].url}" class="card card-product-grid">
						<a href="{$displayRandomProducts[product].url}" class="img-wrap"> <img src="/uploads/{$displayRandomProducts[product].image}"> </a>
						<figcaption class="info-wrap">
							<a href="{$displayRandomProducts[product].url}" class="title" style="height:45px;">{$displayRandomProducts[product].name}</a>
							{if isset($settingCatalogMode)}
								{if $settingCatalogMode != "1"}
								<div class="price mt-1">{{{$displayRandomProducts[product].price}+{$displayRandomProducts[product].price}*20/100}|number_format:2:",":"."} kr.</div> <!-- price-wrap.// -->
								{/if}
							{/if}
						</figcaption>
					</div>
				</div> <!-- col.// -->
				{/section}

			</div> <!-- row.// -->

We havent loaded this yet, but what templateFunctions.php does is finding the variable name $xxxxxxxx and then looking in the directory template-functions for a .php file with that name. if the name exists, it will include the file and its functions. This way you can extend the template with more files and only load them on a the pages where its needed.

Inside the file displayRandomProducts.php we find a lot of code. But the important thing is , it is assigning the products to the variable we are using in the index.tpl file.

Is this not slow to load files this way?

Yes and no. Think of template functions as plugins known from other systems. Except it only loads the functions if they are used. It does check for each file, but that is a very low-performance hit (~20ms).

Clone this wiki locally