olado/doT

ejs way using javascript translated to .doT engine

mikzuit opened this issue · 9 comments

it is possible to do this way I used to use this javascript functions or regex in ejs in .dot? . I explain in this question

<%= blog.created_at.getFullYear() %>
<%= blog.short_name.replace(new RegExp(" ", "g"), "-") %>
<%= blog.content.match(/<img[^>]+src="([^">]+)/)[1] %>

but I have trouble making this work in .dot engine. I know my problem are javascript function, regex and match I'm using. meaning bold part, is not correct here ( getFullYear() , replace(new RegExp(" ", "g"), "-") , match(/]+src="([^">]+)/)[1] )

{{= blog.created_at.getFullYear() }}
{{= blog.short_name.replace(new RegExp(" ", "g"), "-") }}
{{= blog.content.match(/<img[^>]+src="([^">]+)/)[1] }}

thanks in advance

for now I now my problems are double quotes ( " ) , square braquets ( [] ), I will come with a solutions later

{{= blog.created_at.getFullYear() }}
{{= blog.short_name.replace(new RegExp(' ', 'g'), '-') }}

still have to search for a solution for this

{{= blog.content.match(/<img[^>]+src="([^">]+)/)[1] }}

I haven't get a way to replace all chars , I suppose this can be done with ascci o hex chars . but this is a wrong implementation by my side. What I do know is getting that variable in the route before the render.

this string (where I use the regex is inside an array of objects .. so I use underscore to get what I need.

    // mapping a new array
      _.map(array, function(object,key){
      // extending object with a new property
       return _.extend(object, {image: img_sources || 'image'}); 
    });

I don't understand the question. All this should be possible. Can you post one example that doesn't work for you? And what results you are getting.

Is when I use things like this on my templates

{{= blog.short_name.replace(new RegExp(" ", "g"), "-") }} {{= blog.content.match(/<img[^>]+src="([^">]+)/)[1] }}

as I said above, works replacing " (double quotes) for singles , [ ] (square brackets) , don't know what should I use but might work with ascii reference.

hardcoding regexps and such complex logic directly in templates is never a good idea - why can't they be in code? Having said that, it works for me (https://runkit.com/esp/5a71905134dd1300129f172b), so can you please make a one example of full code that doesn't work for you?

what is blog in your case? Isn't it by any chance a property of the data you pass? If so, it should be used as it.blog

about the harcoding: , you are right , that's why I set a solution above (the right in our point of views)

sorry for not saying blog is an object and content is a property (html string) that comes from a iteration like:

{{~ blogs :blog }}
   {{= blog.content.match(/<img[^>]+src="([^">]+)/)[1] }}
{{~}}

it does work when u used when is a evaluation with " .it" but not when is a loop

thanks @epoberezkin

I would repeat - could you please submit a full working example (in JavaScript) together with the output you are getting and the output you expect, as I still don't understand the problem. Please submit it to runkit.com.

Closing until there is failing sample.