RandomEtc / ejs-locals

Express 3.x layout, partial and block template functions for the EJS template engine.
298 stars 63 forks source link

support for absolute url in include #27

Closed andrewtennison closed 11 years ago

andrewtennison commented 11 years ago

I am trying to include an html fragment from a hosted server, this is due to how our decoupled cms works. It does not respect the url and tries to find the asset locally.

<%-include('http://www.website.co.uk/components/footer.html')%>

gets compiled and throws a 500 error due to missing file as: '/localfilepath/http://www.website.co.uk/components/footer.html'

Is there anyway around this?

RandomEtc commented 11 years ago

I removed include from ejs-locals because it is now provided by ejs.

I'd strongly advise against pulling templates from outside your own project, but if you do it you should probably request the templates once and cache them locally. EJS include is done synchronously and cached (async templates are still bleeding edge and not many libraries support them). There's no way to do a synchronous network request in node so what you describe will be impossible without inspecting templates and loading network assets up front.

andrewtennison commented 11 years ago

thanks, these includes are controlled by me, it is just an issue of the company architecture. I am intending to load them upfront and cache once. Any thoughts on how I would load them pre render, possible fs.fileRead?

RandomEtc commented 11 years ago

If they're coming across a network you'll need an HTTP client - node's is OK but a wrapper like request is better. I'm pretty sure with request you can make a stream to download a URL and pipe it into a writable file stream. If you're managing many simultaneous downloads take a look at async or similar to keep things tidy.

Alternatively if you have a startup script then doing rsync or wget --mirror might not be a terrible idea before starting the server.

andrewtennison commented 11 years ago

thanks for the help