user266003
user266003

Reputation:

How to work with html files in snap?

I begin learning snap framework. I'm trying to render an html file and layout html file ("template") in a simple way. That is to say, I have a shared html file I want to use as a template one for all the page in my website. How can I do that? And, a similar question, how can I render an html file?

UPDATE:

I don't want to add a new level of abstruction. Is there any to avoid using Heist?

Upvotes: 1

Views: 441

Answers (1)

mightybyte
mightybyte

Reputation: 7272

The simplest way to serve static files is with serveFile. You would probably have something like this in your routes:

[ ...your routes here
, ("markup", serveFile "mymarkup.html")
]

This makes it so that when a user goes to http://yoursite/markup, the file mymarkup.html in whatever directory you ran the app from gets served. If you are trying to serve many different HTML files, a better solution is serveDirectory. You might use it like this:

[ ...your routes here
, ("static", serveDirectory "html")
]

This makes it so that when a user goes to http://yoursite/static/mymarkup.html, the file html/mymarkup.html gets served.

For your question of how to make one file be the template for all your pages, that is precisely what Heist does for you! There are certainly ways to avoid using Heist, but you will probably be reinventing a lot of what Heist does for you. At the basic level that you're asking about, Heist is pretty straightforward. All valid HTML files are valid Heist templates. I would recommend reading through the tutorial like Bakuriu mentioned. I believe Heist's apply tag is almost exactly what you're looking for.

Upvotes: 1

Related Questions