sqlpage / SQLPage

Fast SQL-only data application builder. Automatically build a UI on top of SQL queries.
https://sql.datapage.app
MIT License
1.57k stars 89 forks source link

Dynamic title #579

Closed amrutadotorg closed 4 weeks ago

amrutadotorg commented 1 month ago

Hi on each page I have

SELECT 'dynamic' AS component,
    sqlpage.run_sql('shell.sql')
    AS properties;

I was wondering how to change the page title based on the visited page. Right now, it’s static. Is there a way to pass a variable (like post_id) to the shell component? I couldn’t find any examples in the documentation.

thank you

my shell sql.sql:

SELECT 'shell' AS component,
    'Learn Sahaja Yoga' AS title,
    'fluid' as layout,
    'images/logo.svg' as image,
    'style.css' as css,
    'manifest.json' as manifest,
    '/fonts/Montserrat.woff2' as font,
    'search' as search_target,
    'favicon.ico' as favicon,
    'images/social_image.webp' as social_image,
    '/js/mark.min.js' as javascript,
    '/js/mark-me.js' as javascript,
    '/js/navigation-buttons.js' as javascript,
    '/js/navigation-meta.js' as javascript,
    '/js/autoSubmit.js' as javascript,
    '/js/shareButton.js' as javascript,
    '/js/lazyload.js' as javascript,
    '/js/open_links.js' as javascript,
    sqlpage.cookie('lightdarkstatus') AS theme,
    '/' AS link,
    '[
        {"title":"All Talks","link":"/all_talks_lang?lang=en"},
        {"title":"Discover","submenu":[
    {"title":"Discover Shri Mataji","link":"//shrimataji.org"},
    {"title":"Discover Sahaja Yoga","link":"//sahajayoga.org"},
    {"title":"Learn meditation","link":"//freemeditation.com"},
    {"title":"Meditate online","link":"//sahajaonline.com"},
    {"title":"WeMeditate","link":"//wemeditate.com"},
    {"title":"Centre near you","link":"//freemeditation.com/meditation-classes/"}
        ]},
        {"title":"Contact","link":"mailto:nirmala.vidyallc@gmail.com"},
        {"title":"Donate","link":"//donate.amruta.org"},
        {"title":"☀","link":"/toggle"}
    ]' AS menu_item,
    18 as 'font_size',
    'Experience of Self-Realisation and teachings of Shri Mataji Nirmala Devi on the practice of Sahaja Yoga and meditation for the transformation of human awareness.' as description,
    'en-US' as language,
    $search as search_value,
    '[Terms & Conditions](pdf/LSY-Terms-Conditions-of-Use.pdf) | [Privacy Policy](/pdf/LSY-Privacy-Policy.pdf) | [Donate ♥](//donate.amruta.org)' AS footer;
lovasoa commented 1 month ago

Any variable that you set before run_sql will be accessible from the sql file being run.

So you could simply do

set title = 'hello';

SELECT 'dynamic' AS component,
    sqlpage.run_sql('shell.sql')
    AS properties;

and in shell.sql

select 'shell' as component, $title as title;
amrutadotorg commented 1 month ago

thank you, it works!

is there a way to set a default title if $title is not set? something like:

SELECT 'shell' AS component, COALESCE($title, 'default_title') AS title;

or

SELECT 'shell' AS component, 
       CASE 
           WHEN $title IS NULL THEN 'default_title' 
           ELSE $title 
       END AS title;
lovasoa commented 4 weeks ago

yes, both should work

amrutadotorg commented 4 weeks ago

It works only if I remove from the shell 18 as 'font_size'

otherwise I'm getting the error Screenshot 2024-09-10 at 10 48 07

lovasoa commented 4 weeks ago

18 as font_size, not 18 as 'font_size'

Single quotes are for string literals, not column names.

amrutadotorg commented 4 weeks ago

thank you