Express Sweet API Reference - v4.0.0
    Preparing search index...

    Function localVars

    Local template variables middleware

    • Mount local variables middleware on Express application.

      Set local variables accessible in Handlebars views. Variables can be accessed in all views as {{var}} or {{{var}}}. Sets up baseUrl and currentPath variables for use in views.

      Local variables set:

      • baseUrl: The base URL for this application (undefined if URL construction fails)
      • currentPath: The current request path

      Parameters

      • app: Express

        Express application instance

      Returns Promise<void>

      // This method is called automatically by express-sweet.mount()
      import localVars from './middlewares/localVars.js';

      await localVars(app);
      // Using in Handlebars templates
      {{#if baseUrl}}
      <a href="{{baseUrl}}/home">Home</a>
      {{/if}}
      {{#if (eq currentPath '/dashboard')}}Active{{/if}}
      // Base URL rewriting in config/config.js
      export default {
      /**
      * Custom base URL rewriting hook, defaults to undefined.
      * The hook receives undefined if URL construction fails.
      * @type {((baseUrl: string|undefined) => string)|undefined}
      */
      rewrite_base_url: baseUrl => {
      if (!baseUrl) return 'http://localhost:3000'; // Fallback
      return `${baseUrl}/admin`;
      }
      };