We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15667
    • 11 Posts
    I would like to put a few values in global variables that I can access from templates or chunks. I’ve spent a lot of time searching for some documentation on this but have not been able to find any. The approach I’ve taken is to use a snippet, "includeFile", to load the page templates and put some calls to $modx->setPlaceholder in the snippet. The placeholders are visible to templates and documents. If a snippet is cached, it can also see the placeholder. However, if a snippet is not cached, the includeFile snippet is never called so the placeholder values are not set. The work around I have implemented is to include the placeholder initialization code in all the non-cacheable snippets I use, which isn’t great.

    Another solution appears to be to create a plugin that gets called on the onWebPageInit event. This seems to happen on every request, so it would be a reliable way of setting the placeholders.

    Can anyone suggest a better way of achieving what I am trying to do?
      • 31136
      • 72 Posts
      Try way I’m using:

      Step 1— Settings Template.
      Create Template and link with it some TVs (this will be settings). I use "ini_" prefix for settings TV.
      FOR EXAMPLE: if you want global setting "color", you have to create TV with name "ini_color", type "dropdown" and elements "red||green||blue"

      Step 2 — Settings Document
      Create document with this Template and set alias to "settings" (Now you can easy edit all your additional settings)

      Step 3 — Include settings into MODx settings array

      A. Open folder manager\includes

      B. In file document.parser.class.inc.php, line 207 (MODx 0.9.6), in getSettings function, after:
      // store base_url and base_path inside config array
      $this->config['base_url']= MODX_BASE_URL;
      $this->config['base_path']= MODX_BASE_PATH;
      $this->config['site_url']= MODX_SITE_URL;
      


      Add:
      include('extra.config.inc.php');
      


      C. in manager\includes create file extra.config.inc.php and fill it by:
      <?php
      // set static variables
      $this->config['ini_currency']	= 'usd';	// Site default currency
      $this->config['ini_phone']	= '+79284517838';	// Phone
      $this->config['ini_address']	= 'Sochi, Mira street, 21';	// Address
      
      // set variables from settings page
      $site_content = $this->getFullTableName('site_content');
      $site_tmplvars = $this->getFullTableName('site_tmplvars');
      $site_tmplvar_contentvalues = $this->getFullTableName('site_tmplvar_contentvalues');
      $sql = "SELECT tv.name,tvc.value FROM $site_tmplvar_contentvalues AS tvc
      		LEFT JOIN $site_tmplvars AS tv ON tv.id=tvc.tmplvarid
      		LEFT JOIN $site_content AS c ON c.id=tvc.contentid
      		WHERE c.alias='settings';";
      $rs = $this->db->query($sql);
      while($row = $this->db->getRow($rs)) {
       $this->config[$row['name']] = $row['value'];
      }
      mysql_free_result($rs);
      ?>
      


      This little script links your static settings and settings from document with alias "settings" with MODx settings array and you can use it everywere by calls:
      1.
      $modx->config['ini_mysetting']
      — in snippets/modules/plugins
      2.
      [(ini_mysetting)]
      — in documents/templates/chunks

      Add settings how you need by linking new TVs to Settings Template and Enjoy!
      Good Luck!