We're evolving to serve you better! This current forum has transitioned to read-only mode. For new discussions, support, and engagement, we've moved to GitHub Discussions.

Global String Variable?

  • #7533
    Avatar photo[anonymous]


    A website we built with Publii has functions for people, for example “Project Manager” that is mentioned on multiple pages. Is there a way to turn a string like that in a global variable so it would be easier to edit it in one place (for example to “PM”) and it’s automatically updated in all places?

    I saw @customHTML global variable: – but this doesn’t seem viable if you have for example 50+ variables? And it also seems like you can’t simply add “@variableName” in a post text editor?

    Thanks for the help and/or input!

    Avatar photo[anonymous]


    Not sure about the global string variable, but below is another method I use to make potentially hundreds of changes in seconds on a big Publii websites.

    To replace all instances of the text ‘Project Manager’ with ‘PM’ — both without the quotes.

    You can use the free software DB Browser for SQLite. I’m using an Apple Mac.

    Naturally, before doing the change below, do keep an unchanged backup copy of your Publii website db.sqlite file — just in case something goes wrong.

    Then, close Publii and open the website db.sqlite file you want to change in DB Browser for SQLite.

    Choose the ‘Execute SQL’ tab in DB Browser for SQLite.

    Then copy and paste the following command into the box:

    UPDATE posts SET text = REPLACE(text, 'Project Manager', 'PM')

    Choose the ‘Execute all/selected SQL’ button.

    Choose the ‘Write Changes’ button.

    Exit DB Browser for SQLite.

    Open your Publii site again.

    Your changes should be made whether your site has 5 pages or 500 pages.

    The above takes command takes seconds and beats making changes one by one (hours or days).

    Avatar photoBob

    I suggest creating a dedicated helper in a few steps:

    1. Create a helpers.js file ‎⁨(in this location: Documents ▸ ⁨Publii⁩ ▸ ⁨sites⁩ ▸ ⁨YOUR_SITE ▸ ⁨input⁩ ▸ ⁨themes⁩ ▸ ⁨YOUR_THEME) with the following code snippet:
      let themeHelpers = {
        replaceContent: function(postText) {
            let modifiedPostText = postText;
               modifiedPostText = modifiedPostText.replace('Project Manager', 'PM');     
               modifiedPostText = modifiedPostText.replace('Webdesigner', '<button class="buy-now-btn">Buy Now</button>');           
            return modifiedPostText;
      module.exports = themeHelpers;
    2. Edit the post.hbs file as follows:
      Replace the following part of the code: {{{text}}} with {{{replaceContent text}}}
    3. That’s it.
      Now you can add multiple variables in the helpers.js file by adding a new line starting with “modifiedPostText = …

    To preserve your custom code and prevent it being overwritten the next time the theme is updated, please ensure that you have created override files; these are, essentially, copies of the existing theme files that are stored separately from the main theme files

    Avatar photo[anonymous]


    Thanks for the tip Bob.

    Your method is a lot more safer than what I’ve done in the past (playing with db.sqlite outside of Publii) and is more in keeping with the Publii way of doing things. Could be really handy for the rare occasions and should save a lot of time.

    Avatar photo[anonymous]

    Thank you @Bob (and sorry for the late reply). That’s a great example! I can put this as an option on the table here.