Create global year variable to use in title & texts

Hi, is it possible for me to create something like currentYear variable and use it everywhere.

in meta title and component texts i want to use something like this.
(xxx 2025 xxx)

Hi @Ischafak,

In Builder.io, you indeed have the capability to use dynamic data like the current year, accessed through JavaScript or custom fields.

Here’s an overview of how you can implement this:

  1. Creating a Custom Field:

    • You can add a custom field to your model (like a page or section model) and name it currentYear.
    • You can set this field programmatically in your integration, or update it manually to the current year.
  2. Accessing in Integration Code:

    • In your integration code, fetch the custom field data and use it where needed:
    // Access custom field data
    const pageContent = await builder.get('page', { /* query parameters */ }).toPromise();
    const currentYear = pageContent?.data?.currentYear; // Assuming 'title' is the field containing the year
    
  3. Using JavaScript in Custom Code within Content:

    • If you’re using JavaScript within your custom code snippets in the content, you can dynamically set the year and use it throughout your components:
    const currentYear = new Date().getFullYear();
    builder.set('currentYear', currentYear);
    
  4. Binding Current Year in Content:

    • Use the context object to bind the value within your content: The current year is {{context.builderContent.data.currentYear}}

Hope this helps!

Thanks,