Sign in

Global String Variable?

  • This topic has 4 replies, 3 voices, and was last updated 3 weeks, 2 days ago by .
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #7533
    Moizo

    Hello,

    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: https://getpublii.com/dev/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!

    #7534
    itips3727

    @Moizo

    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).

    #7535
    Bob

    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 fileshttps://getpublii.com/dev/theme-overrides/

     

    --
    Do you appreciate the support you've received today? If so, consider donating to the Publii team by clicking here; we'll be sure to use your donation to make Publii even better!

    #7536
    itips3727

    @Bob

    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.

    #7554
    Moizo

    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.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.