Global String Variable?
-
June 2, 2022 at 2:23 pm #7533[anonymous]
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!
June 2, 2022 at 5:33 pm #7534[anonymous]@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).
June 2, 2022 at 5:55 pm #7535BobI suggest creating a dedicated helper in a few steps:
- 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;
- Edit the post.hbs file as follows:
Replace the following part of the code: {{{text}}} with {{{replaceContent text}}} - 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 → https://getpublii.com/dev/theme-overrides/
June 3, 2022 at 2:08 pm #7536[anonymous]@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.
June 6, 2022 at 2:14 pm #7554[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.
- Create a helpers.js file (in this location: Documents ▸ Publii ▸ sites ▸ YOUR_SITE ▸ input ▸ themes ▸ YOUR_THEME) with the following code snippet: