简体   繁体   English

在模板目录之外使用Heist渲染模板

[英]Rendering templates with Heist outside of the templates directory

I'm using Snap to create a fairly simple portfolio that, for the most part, just stores stuff in the database and shows it to the user. 我正在使用Snap创建一个相当简单的投资组合,在大多数情况下,它们只是将内容存储在数据库中并显示给用户。 One of the features I'd like to have is the ability to show off retired designs for my portfolio. 我想拥有的功能之一是能够展示我的投资组合中已淘汰的设计。 Each design would be little more than a single template and a handfull of assets (images, css, etc.). 每个设计都只不过是一个模板和少量资产(图像,css等)。 For organizational purposes, I would like to keep everything belonging to a single design together and separate from the templates/assets for my portfolio. 出于组织目的,我希望将属于单个设计的所有内容放在一起,并与投资组合的模板/资产分开。

src/Site.hs

static/images/logo.png
static/css/responsive.css

archives/foo.com/2012-03/index.html
archives/foo.com/2012-03/images/logo.png
archives/foo.com/2012-03/css/styles.css
archives/foo.com/2012-03/favicion.ico

archives/bar.com/2011-08/index.html
archives/bar.com/2011-08/images/logo.png
archives/bar.com/2011-08/css/styles.css
archives/bar.com/2011-08/favicion.ico

I did try using serveDirectory on archives . 我确实尝试在archives上使用serveDirectory Requesting example.com/bar.com/2012/03/ requests archives/bar.com/2012/03/index.html as one would expect and that's fine for some instances. 如所期望的那样,请求example.com/bar.com/2012/03/请求archives/bar.com/2012/03/index.html ,这在某些实例中很好。 I would like to be able to use some compiled splices or Charade so that the page doesn't look so empty when the original content can't be replicated (usually because it came from a database that's long forgotten). 我希望能够使用一些已编译的剪接或Charade ,以便在无法复制原始内容时(通常因为它来自一个长期被遗忘的数据库),页面不会显得很空。

Maybe making a separate snaplet for this purpose makes more sense? 也许为此目的单独制作一个Snaplet更有意义吗? If so, how would I go about doing this? 如果是这样,我将如何去做? For reference, my site's snaplet is fairly basic and looks something like this: 作为参考,我网站的快照非常基本,看起来像这样:

app :: SnapletInit App App
app = makeSnaplet "connex" "A snaplet for the connex site." Nothing $ do
    h <- nestSnaplet "heist" heist $ heistInit' "templates" defaultHeistState
    s <- nestSnaplet "session" sess $ initCookieSessionManager "config/site_key.txt" "session" (Just 86400)
    d <- nestSnaplet "db" db pgsInit

    addRoutes 
        [ ("/", indexH siteH)
        -- more routes here
        , ("", serveDirectory "static")
        ]
    return $ App h s d
    where
        defaultHeistState = mempty {
            hcInterpretedSplices = defaultInterpretedSplices,
            hcLoadTimeSplices = defaultLoadTimeSplices
        }

(PS I have a similar but unrelated project that allows users to customize the appearance of their own "site". Currently, customization is limited to images and CSS. If the solution for the above problem could be used to allow customizing the layout template for each user, that would be great. If not, no worries.) (PS我有一个类似但不相关的项目,允许用户自定义他们自己的“站点”的外观。目前,自定义仅限于图像和CSS。如果可以使用上述问题的解决方案来自定义布局模板,每个用户,那就太好了。如果没有,请不要担心。)

You'll probably have to do some manual wiring to get this to work the way you want, but there are some helpers that you can use. 您可能必须进行一些手动接线才能使其按您想要的方式工作,但是您可以使用一些帮助器。 First there is the addTemplatesAt function that lets you include external templates in your HeistState. 首先,有addTemplatesAt函数,可让您在HeistState中包括外部模板。 You can use that in combination with your own serveDirectory routes to serve the static resources. 您可以将其与您自己的serveDirectory路由结合使用,以提供静态资源。 Once you get this working for one case, I'm sure you'll be able to find a way to combine the two in an abstraction that lets you pretty easily add multiple versions of your site's previous look. 一旦解决了这种情况,我相信您将能够找到一种将两者结合为抽象的方法,该方法使您可以轻松地添加站点以前外观的多个版本。

Here's the detailed solution I ended up with for my primary use case: 这是我针对主要用例最终得到的详细解决方案:

app :: SnapletInit App App
app = makeSnaplet "app" "An snaplet example application." Nothing $ do
    h <- nestSnaplet "heist" heist $ heistInit' "templates" defaultHeistState
    s <- nestSnaplet "sess" sess $ initCookieSessionManager "site_key.txt" "sess" (Just 3600)
    d <- nestSnaplet "db" db pgsInit

    addRoutes routes
    addTemplatesAt h "archives" "archives" -- added this
    return $ App h s d
    where
        defaultHeistState = mempty
            { hcInterpretedSplices = defaultInterpretedSplices
            , hcLoadTimeSplices = defaultLoadTimeSplices
            }

The handler to serve my templates looks like this (very similar to what heistServe looks like): 为我的模板提供服务的处理程序如下所示(非常类似于heistServe外观):

archiveServe :: AppHandler ()
archiveServe = do
    url <- withRequest (return . rqPathInfo)
    let
        splices = return ()
        template = "archives/" <> url <> "index"
    renderWithSplices template splices 

And my routes: 而我的路线:

designH = route
    [ ("/", ifTop designIndexH)
    , ("/archives/", archiveServe)
    , ("/archives/", serveDirectory "archives")
    ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM