Jump to content

Welcome, Guest!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Theo

[Enhancement] Static to Dynamic URL Maker with Limited Smarty Tags in CMS. Works in Top Menu, CMS and TB HTML Block

Recommended Posts

Hi

As we needed dynamic URLs in the Menu, CMS pages and TB HTML Block, and TB doesn't have this functionality natively out the box, I've decided to write a little 'workaround' / solution to try and fix this issue
(at least temporarily until TB one day does this natively).

Solution: Static URLs to Dynamic URLs maker + Smarty Tags in CMS, Menu and TB HTML Block

  • The problem with static URLs is that they are a pain to maintain especially if everything you added to the site in the CMS is saved as a Static URL... 
  • So if you need to move the site from Local -> Staging -> Live and to another domain, you're going to have a bit of a problem.
  • I've had to 'fix' this for the Top Menu, CMS page and TB HTML Block
  • This solution also allows you to 'call' Smarty variables in the Admin BO CMS, Top Menu and TB HTML Blocks.

How does it work and how to use?

  • Add the code to either blocktopmenu.tpl, blockcms.tpl in your template file or tbhtmlblock.tpl - replacing the content generator: either {$MENU} or {cms->content} or {$customblock}
  • Specify on which page (Menu/CMS/TB HTMLBlock) the code needs to appear {$MENU} or {cms->content} or {$customblock}
  • The code replaces the "content generation" for the target page / section
  • You need to specify the Static URLs you want to replace (remember to do this wherever the script is added). Static URLs normally for Local, Staging or Old Live addresses (can add 3 more if required)
  • If you know what the Staging and Live Locations are going to be beforehand, then you do not need to update this again after the site has been moved / re-deployed. 
  • You can call a predefined Smarty variable in the Menu, CMS or TB HTML Block using the Smarty tag identifier {$content_dir} for example.
  • When you add a link, you can either just use the full static path (as previously specified), or use the Smarty variable {$content_dir}blog/link-to-page
  • If you add an image via the CMS RTE or even a link to an image, it will identify the Static URL and replace it with the Dynamic URL as set by $content_dir.
  • So if you move the site to another address, all your Static URLs (if specified in the Menu, CMS or TB HTML Block) will be automatically updated - with no maintenance required from your side.

Credits:

Hope it helps.

Instructions:

  • Insert the following code in either "nameoftemplate/" blocktopmenu.tpl or blockcms.tpl or modules/tbhtmlblock/views/templates/hook/tbhtmlblock.tpl
  • And make sure that it replaces the "page content generator" {$MENU} or {cms->content} or {$customblock}
  • Configure as mentioned previously
  • I've included a 'tester' code you can add to a CMS page to test that it's working properly

Main Script (for tpl files):

{* 
/// STATIC TO DYNAMIC URL MAKER - with 'Dynamic Tags' / 'Fake' Smarty Variables Functionality ///
* v1.1 - last updated: 11/10/19
* To be used on BO Admin pages where Dynamic URLs are required 
* Automagically Replaces Static URLs on Runtime with 'dynamic' URL based on $content_dir 
* So if your site is on Local, Staging, Live or has been moved, this script has you covered
* If you know what the Staging and Live Locations are going to be beforehand, then you do not need to update this again after the
* site has been moved / re-deployed. 
* Just remember to change the 'Static URL Sources' whereever you've included this script
*
* CAVEAT: This does not save the updated safe URLs, it just replaces them on run-time for the front-end.
* CAVEAT: The links and images pointing to the old URLS will still be 'broken' in BO Admin CMS until manually updated.
*
* Allows for the use of limited Smarty variable 'printing' within the 'code location' (CMS,TB HTML Block,Menu,etc)
* I.e. using {$content_dir} in a CMS page will now work (like in a TPL file)
* This code should be placed on the Target location's TPL file like "blocktopmenu.tpl, blockcms.tpl" within your theme
* or for the TB HTML Block: modules/tbhtmlblock/views/templates/hook/tbhtmlblock.tpl
* The Code Location must be changed accordingly below
* Extendable by adding more Smarty variables or code locations
* Note: If you want to have 'BaseUrl in a TPL page, just use the normal Smarty var $content_dir as this script is built for BO Admin pages
* ie. <a href="{$content_dir}info/link-to-cms-page">cms page link</a>
*
* Credit to FullCircles for initial 'BaseURL' solution back in 2014. Without you, we'd be f'ed. Thanks bro :D
* https://www.prestashop.com/forums/topic/372976-global-smarty-variables-used-in-cms-content/?tab=comments#comment-1855331
* https://www.prestashop.com/forums/profile/720051-fullcircles/
* 
* Credit to Datakick for the Smarty variable for the correct site domain path: $content_dir  
*
* // To use:
* Adjust the settings (values) below and add Smarty variables etc if you require
* Replace the page's content generator tag, i.e. $cms->content, with this entire script block. 
* Refresh Cache
* Static URLs as specified should now be replaced and 'dynamic' tags should be displayed
* Note: the URLs come with a trailing slash (pagename/) so keep that in mind
* 
*}

{* /// Initial build, do not modify this one - Creates 6 available Static URL slots if you need them /// *}
{assign var=s2d_specifysourceurls value=['placeholder-donotchangethis0','placeholder-donotchangethis1','placeholder-donotchangethis2','placeholder-donotchangethis3','placeholder-donotchangethis4','placeholder-donotchangethis5','placeholder-donotchangethis6']}
{* /// /// *}

{* /// Set Static URL Sources (array with 3 values for now - supports up to 56): ///
- Remember to include trailing slash (pagename/)
- Example array currently set for Local, Staging, Live Old  
- Example: {$s2d_specifysourceurls[0] = 'http://localhost/siteaddress/'}
*}

{* /// Set Local Address Source - Change this /// *}
{$s2d_specifysourceurls[0] = 'http://localhost/siteaddress/'}
{* /// Set Staging Address Source /// *}
{$s2d_specifysourceurls[1] = 'http://www.stagingaddress.com/'}
{* /// Set Old  Live Address Source /// *}
{$s2d_specifysourceurls[2] = 'http://www.liveaddress-old.com/'}
{* /// Extendable if required - up to 6 available slots: (0-5)  //*}
{* {$s2d_specifysourceurls[3] = 'http://www.emptyslot4.com'}
{$s2d_specifysourceurls[4] = 'http://www.emptyslot5.com'}
{$s2d_specifysourceurls[5] = 'http://www.emptyslot6.com'} *}


{* /// Set Dynamic URL source, leave as $content_dir for dynamic URL. Only 1 URL required /// *}
{assign var=s2d_url_maker_target value="{$content_dir}"} 


{* /// Set Code Location ///
- Set the Code Location by changing the value below accordingly:
- CMS page: {$cms->content}
- TB HTML BLOCK: {$customblock}
- MENU: {$MENU}
*}
{assign var=s2d_url_maker_code_location value="{$MENU}"} 

{* /// Generator that does the work: ///
- You can add more variables to the beginning of the source and target arrays below if required 
- Default list of available Smarty variables:
- $content_dir, $base_uri, $base_dir, $base_dir_ssl, $img_dir (template img dir bytw), 
- $page_name, $logo_url, $shop_name, $shop_phone
*}

{* /// Generator /// *}
{str_replace(
array('{$content_dir}','{$base_uri}','{$base_dir}','{$base_dir_ssl}','{$img_dir}','{$page_name}','{$logo_url}','{$shop_name}','{$shop_phone}',$s2d_specifysourceurls[0],$s2d_specifysourceurls[1],$s2d_specifysourceurls[2],$s2d_specifysourceurls[3],$s2d_specifysourceurls[4],$s2d_specifysourceurls[5]),
array($content_dir,$base_uri, $base_dir, $base_dir_ssl, $img_dir, $page_name, $logo_url, $shop_name, $shop_phone, $s2d_url_maker_target,$s2d_url_maker_target,$s2d_url_maker_target,$s2d_url_maker_target,$s2d_url_maker_target,$s2d_url_maker_target), $s2d_url_maker_code_location
)}

{* /// *** *** /// *}

 

Tester Code:

<h2>Static 2 Dynamic URL Maker with Limited 'Smarty' Tags in CMS Tester</h2>
<br/>
<b>Static URL Locations to be updated:</b>
<br/>
<br/>
<b>Local: </b> http://localhost/siteaddress/
<br/>
<b>Staging: </b> http://www.stagingaddress.com/
<br/>
<b>Live old/prev: </b> http://www.liveaddress-old.com/
<br/><br/>
<b>Smarty variable print:</b>
<br/>
<br/>
<b>$content_dir:</b> {$content_dir}
<br/>
<b>$base_uri:</b> {$base_uri}
<br/>
<b>$base_dir:</b> {$base_dir}
<br/>
<b>$base_dir_ssl:</b> {$base_dir_ssl}
<br/>
<b>$img_dir:</b> {$img_dir} 
<br/>
<b>$page_name:</b> {$page_name}
<br/>
<b>$logo_url:</b> {$logo_url}
<br/>
<b>$shop_name:</b> {$shop_name}
<br/>
<b>$shop_phone:</b> {$shop_phone}

 

Script not active yet:

script not yet implemented.png

Script activated, Static URLs + Smarty Tags updated + Menu Links updated

script implemented.png

admin top menu.png

Edited by Theo
Improved script to handle Local, Staging, Live URL + more static urls + TB HTML Block
  • Thanks 2

Share this post


Link to post
Share on other sites

CAVEAT:
The script does not save the updated safe URLs, it just replaces them on run-time for the front-end display. 
The links and images pointing to the old URLs will still be 'broken' in BO Admin CMS until manually updated.

Edited by Theo

Share this post


Link to post
Share on other sites

I've written a Script that fixes the broken URLs and images issue for CMS content:

CMS Old URL to New URL replacer Script for TinyMCE

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...