Solved

Function libraries for email templates?

  • 15 September 2023
  • 1 reply
  • 90 views

Badge

Is there a way to maintain libraries of our own functions for use in the email templates?

“Don’t Repeat Yourself” (DRY) is an important programming principle, but our templates are built with a lot of cut-and-paste snippets.

For example, we frequently use:

{{ person|lookup:"VehicleModel"|default:'ride' }}

...to say things like “new items for your Toyota Tundra”. This is sprinkled through a great many templates.

The right way to do this is with a function, something like:

def getVehicle(person)

  # return person|lookup:"VehicleModel"|default:'ride' 
person.fetch("VehicleModel", "ride")

end

...and then just

{{ getVehicle(person) }}

in the template itself.  It’s short, simple, reusable.

is this syntax, or something like it, possible? Ruby, Python, Javascript - I don’t care which, any reasonable language will do. And can functions be placed into a file to be shared and reused across all our templates?

 

icon

Best answer by David To 15 September 2023, 21:56

View original

1 reply

Userlevel 7
Badge +60

Hey @aagmatt,

Welcome to the Klaviyo Community! 

Seems like you may have overlooked our universal and saved content features which are meant do do just what you’re describing. 

For blocks you used often, you can always save them by hovering and selecting the star icon. Once saved, you can access and use this exact block in any other template your editing by finding the saved block under the Universal tab. 

You can also take it a step further and make changes to these saved blocks, which can then be reflected across all other templates using the saved block - so long as they’re linked. 

Also, instead of copy and pasting custom profile variables, I would suggest using the Add personalization menu within a text block. I find that this makes personalizing emails a lot easier. 

Overall, you wouldn’t even need to actually make any functions, all you have to do is save the block you want!

David

Reply