{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","results":{"codes":[]},"settings":"","params":[]},"next":{"description":"","pages":[]},"title":"Views","type":"basic","slug":"views","excerpt":"","body":"Views return HTML data from your application. They can be created from pure HTML documents or passed through renderers such as Mustache or Stencil.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Views Directory\"\n}\n[/block]\nViews are stored in `Resources/Views`. This directory can be accessed via the views directory property on the application. This builds the path to the view directory from the working directory.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"app.viewsDir\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"HTML\"\n}\n[/block]\nReturning HTML, or any other non-rendered document, is simple. Just pass the path to the document to `View(path: String)`. This path is relative to `viewsDir`.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"app.get(\\\"/\\\") { request in\\n    return try View(path: \\\"index.html\\\")\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Throws\",\n  \"body\": \"This call throws a `View.Error.InvalidPath` if it cannot find the file. You can catch this in the closure, or in middleware.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Public Resources\"\n}\n[/block]\nAny resources that your views need, such as images, styles, and scripts, should be placed in the `Public` folder at the root of your application. \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"View Renderer\"\n}\n[/block]\nAny class that conforms to `ViewRenderer` can be set to render views with a given context.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"class MustacheRenderer: RenderDriver {\\n    ...\\n}\\n\\nView.renderers[\\\".mustache\\\"] = MustacheRenderer()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThis will pass any files that end with `.mustache` through the mustache renderer before returning them.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Available Renderers\"\n}\n[/block]\nThese renderers can be added to your application through Providers.\n\n# Mustache\n- [Zewo Mustache](https://github.com/qutheory/vapor-zewo-mustache)\n\n# Stencil\n- [Qutheory Stencil](https://github.com/qutheory/vapor-stencil)","updates":["5734935af5b86d0e00a7650f"],"order":3,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5768701b72cd4c0e000fad20","parentDoc":null,"githubsync":"","__v":0,"user":"57086f3d447c410e00221bd1","version":{"version":"0.12","version_clean":"0.12.0","codename":"Client Clyde","is_stable":false,"is_beta":true,"is_hidden":false,"is_deprecated":true,"categories":["5768701b72cd4c0e000fad19","5768701b72cd4c0e000fad1a","5768701b72cd4c0e000fad1b","5768701b72cd4c0e000fad1c"],"_id":"5768701b72cd4c0e000fad18","releaseDate":"2016-06-20T22:37:15.641Z","__v":1,"createdAt":"2016-06-20T22:37:15.641Z","project":"57086fe855a4690e005de407"},"category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Guide","slug":"getting-started","order":1,"from_sync":false,"reference":false,"_id":"5768701b72cd4c0e000fad1a","createdAt":"2016-04-09T04:31:52.489Z","__v":0,"version":"5768701b72cd4c0e000fad18","project":"57086fe855a4690e005de407"},"createdAt":"2016-04-09T20:38:37.838Z","project":"57086fe855a4690e005de407"}
Views return HTML data from your application. They can be created from pure HTML documents or passed through renderers such as Mustache or Stencil. [block:api-header] { "type": "basic", "title": "Views Directory" } [/block] Views are stored in `Resources/Views`. This directory can be accessed via the views directory property on the application. This builds the path to the view directory from the working directory. [block:code] { "codes": [ { "code": "app.viewsDir", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "HTML" } [/block] Returning HTML, or any other non-rendered document, is simple. Just pass the path to the document to `View(path: String)`. This path is relative to `viewsDir`. [block:code] { "codes": [ { "code": "app.get(\"/\") { request in\n return try View(path: \"index.html\")\n}", "language": "swift" } ] } [/block] [block:callout] { "type": "warning", "title": "Throws", "body": "This call throws a `View.Error.InvalidPath` if it cannot find the file. You can catch this in the closure, or in middleware." } [/block] [block:api-header] { "type": "basic", "title": "Public Resources" } [/block] Any resources that your views need, such as images, styles, and scripts, should be placed in the `Public` folder at the root of your application. [block:api-header] { "type": "basic", "title": "View Renderer" } [/block] Any class that conforms to `ViewRenderer` can be set to render views with a given context. [block:code] { "codes": [ { "code": "class MustacheRenderer: RenderDriver {\n ...\n}\n\nView.renderers[\".mustache\"] = MustacheRenderer()", "language": "swift" } ] } [/block] This will pass any files that end with `.mustache` through the mustache renderer before returning them. [block:api-header] { "type": "basic", "title": "Available Renderers" } [/block] These renderers can be added to your application through Providers. # Mustache - [Zewo Mustache](https://github.com/qutheory/vapor-zewo-mustache) # Stencil - [Qutheory Stencil](https://github.com/qutheory/vapor-stencil)