Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7957803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:19:00+00:00 2026-06-04T04:19:00+00:00

Is there a best case practice for validating when using a templating engine? I’m

  • 0

Is there a best case practice for validating when using a templating engine?

I’m currently building a site with node.js and couchdb, and I’m using express with ejs as a framework and templating engine respectively. It’s possible that I end up with [Object object] or undefined spelled out literally in my HTML. I can try to validate in couch with a validate_doc_update function, or in node inside my routing function before I render to the template, or right in the ejs template. Which one of these or all of these should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:19:01+00:00Added an answer on June 4, 2026 at 4:19 am

    I suggest writing a function to take your template context object and prepare it for rendering. These are sometimes called presenter functions and/or the presenter pattern. You would use this from your node.js routing function before your render the template. This function could replace null/undefined with empty string and also detect objects whose toString is “[Object object]” or otherwise undesired and replace them with empty string as well. It is easy to write unit tests for this and will prevent you from having to duplicate logic in many templates. This function should recursively walk an entire object graph or array of object graphs.

    You may also want other functionality like abbreviating abnormally long strings. For example, if someone accidentally pastes in a bunch of garbage into the “firstName” field of your user object and this is over some sane limit of say 100 characters, your presenter function could truncate it and append an ellipsis. Just another example of this data cleanup or “presentation” type logic.

    Otherwise, you need expressions like <%= someObj.someProp || '' %> in your ejs templates which will cause lots of boilerplate code duplication.

    Here’s a first-cut working jsfiddle implementation (uses underscore.js).

    function sanitize(context) {
        if (Array.isArray(context)) {
          return _.map(context, sanitize);
        }
        _.each(context, function (value, key) {
            if (value === null) {
              context[key] = "";
              return;
            }
    
            switch (typeof value) {
              case "object":
                context[key] = sanitize(value);
                break;
              case "number":
                //No-op
                break;
              case "boolean":
                //No-op
                break;
              default:
                context[key] = value || ""; //handles undefined
            }                
        });
        return context;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a best practice or widely accepted way of structuring and validating data
I'm just wondering if there is a best case to write this code: $('#set_duration_30').click(function(event)
Is there best practice for managing Object shared by 2 or more others Object.
Is there a best practice for making private methods in classes static? I have
Is there a best practice in regards to returning (or NOT returning datasets) from
is there any best practice way to replace a part of the default template.
Is there a best practice for preventing mixed eol-styles in a subversion repository. I
I wonder, what is the best practice for object validation. Is there any extra
Is there a best practice for where configuration files should be stored in a
Is there a Django ORM best practice for this SQL: REPLACE app_model SET field_1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.