{"category":{"version":"58c81e8e693cdd1900606acf","project":"542fe92a5eceb608003fddc8","_id":"58c81e8e693cdd1900606ad1","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-05-13T19:08:44.260Z","from_sync":false,"order":1,"slug":"context-reference","title":"Context Reference"},"parentDoc":null,"project":"542fe92a5eceb608003fddc8","user":"542c5cfcddd3190e00228849","version":{"__v":1,"_id":"58c81e8e693cdd1900606acf","project":"542fe92a5eceb608003fddc8","createdAt":"2017-03-14T16:47:10.620Z","releaseDate":"2017-03-14T16:47:10.620Z","categories":["58c81e8e693cdd1900606ad0","58c81e8e693cdd1900606ad1","58c81e8e693cdd1900606ad2","58c81e8e693cdd1900606ad3","58c81e8e693cdd1900606ad4","58c81e8e693cdd1900606ad5"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"0.11.7","version":"0.11.7"},"_id":"58c81e8f693cdd1900606b0e","__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-05-19T10:24:31.563Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":16,"body":"Use: `{{#is \"subscribe\"}}{{/is}}` to detect this context.\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Context\",\n    \"h-1\": \"Url\",\n    \"h-2\": \"Template\",\n    \"h-3\": \"Data\",\n    \"h-4\": \"Body Classes\",\n    \"0-0\": \"subscribe\",\n    \"0-3\": \"success (boolean)\\nerror (object)\\nemail (string)\",\n    \"0-1\": \"/subscribe/\",\n    \"0-2\": \"subscribe.hbs\",\n    \"0-4\": \".subscribe-template\"\n  },\n  \"cols\": 5,\n  \"rows\": 1\n}\n[/block]\n## Description\n\nIf a blog has the \"subscribers\" feature enabled in labs, then a theme gets access to several additional features for outputting a subscribe page and forms.\n\nThis page is an overview of the \"context\" properties, if you are looking for in depth docs on how to add support to your theme please see the [subscribers feature](doc:subscribers) page.\n\nNote: if subscribers is not enabled, none of the features on this page will be accessible.\n\n## Routes\n\nThe `subscribe` context lives at the matching `/subscribe/` route. This cannot be customised at present.\n\n## Templates\n\nBy default, the `subscribe` context will be rendered using the `subscribe.hbs` file which exists in [/subscribers/lib/views/](https://github.com/TryGhost/Ghost/blob/0.8.0/core/server/apps/subscribers/lib/views/subscribe.hbs). By default, this page is styled to match the Ghost login screen.\n\nTo customise this page, theme developers can provide their own `subscribe.hbs` file as part of their theme. This will override the default.\n\n## Data\n\nThere are 3 pieces of data passed to the `subscribe.hbs` template:\n\n- **success** (boolean) - is true if this page is being rendered after a successful subscribe\n- **error** (object) - has a `message` property if something went wrong when the form was submitted\n- **email** (string) - the email address that was submitted \n\n## Helpers\n\nThe `subscribe` context makes use of a specialist helper called `{{subscribe_form}}`. Documentation for this helper can be found on the [subscribers feature](doc:subscribers) page.\n\n## Example Code\n\nThe `subscribe.hbs` template is intended to have a basic structure which displays a different result depending on whether the form has been successfully submitted or not. \n\nThe following example shows how to display the form if success is false, or otherwise output a success message:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{{#if success}}\\n  <header>\\n    <h1>Subscribed!</h1>\\n  </header>\\n\\n  <p>\\n    You've successfully subscribed to <em>{{:::at:::blog.title}}</em>\\n    with the email address <em>{{email}}</em>.\\n  </p>\\n{{else}}\\n\\t<header>\\n    <h1>Subscribe to {{@blog.title}}</h1>\\n  </header>\\n\\n  {{subscribe_form autofocus=\\\"true\\\"}}\\n{{/if}}\",\n      \"language\": \"handlebars\"\n    }\n  ]\n}\n[/block]\nThe default setup assumes that you'll do any error handling you need within the form itself, so the outer page template can just handle the success / no success case. \n\nHowever, if you want to add some additional template logic based on the error case, it is totally possible to use an 'if - else if - else' structure with handlebars, like this:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{{#if success}}\\n    <header>\\n        <h1>Success!</h1>\\n    </header>\\n{{else if error}}\\n    <header>\\n        <h1>Error!</h1>\\n    </header>\\n\\n    {{subscribe_form autofocus=\\\"true\\\"}}\\n{{else}}\\n    <header>\\n        <h1>Subscribe.../h1>\\n    </header>\\n\\n    {{subscribe_form autofocus=\\\"true\\\"}}\\n{{/if}}\",\n      \"language\": \"handlebars\"\n    }\n  ]\n}\n[/block]\nIf you're a big fan of minimising your templates, there's a trick you can use from the express-hbs library that Ghost uses called \"content blocks\". This allows you to define regions of your template, and define the content for those regions separately, although it requires that the content be defined first to work properly:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{{#if success}}\\n  {{#contentFor \\\"title\\\"}}Success!{{/contentFor}}\\n  {{#contentFor \\\"body\\\"}}<p>You've successfully subscribed :)</p>{{/contentFor}}\\n{{else if error}}\\n  {{#contentFor \\\"title\\\"}}Error!{{/contentFor}}\\n  {{#contentFor \\\"body\\\"}}{{subscribe_form autofocus=\\\"true\\\"}}{{/contentFor}}\\n{{else}}\\n  {{#contentFor \\\"title\\\"}}Subscribe..{{/contentFor}}\\n  {{#contentFor \\\"body\\\"}}{{subscribe_form autofocus=\\\"true\\\"}}{{/contentFor}}\\n{{/if}}\\n\\n<section class=\\\"subscribe\\\">\\n<header>\\n  <h1>{{{block \\\"title\\\"}}}</h1>\\n</header>\\n\\n{{{block \\\"body\\\"}}}\\n\\n</section>\",\n      \"language\": \"handlebars\"\n    }\n  ]\n}\n[/block]\nIf you're looking for more documentation on the `{{subscribe_form}}` helper, please see the the [subscribers feature](doc:subscribers) page.","excerpt":"The `subscribe` context","slug":"subscribe-context","type":"basic","title":"subscribe"}

subscribe

The `subscribe` context

Use: `{{#is "subscribe"}}{{/is}}` to detect this context. [block:parameters] { "data": { "h-0": "Context", "h-1": "Url", "h-2": "Template", "h-3": "Data", "h-4": "Body Classes", "0-0": "subscribe", "0-3": "success (boolean)\nerror (object)\nemail (string)", "0-1": "/subscribe/", "0-2": "subscribe.hbs", "0-4": ".subscribe-template" }, "cols": 5, "rows": 1 } [/block] ## Description If a blog has the "subscribers" feature enabled in labs, then a theme gets access to several additional features for outputting a subscribe page and forms. This page is an overview of the "context" properties, if you are looking for in depth docs on how to add support to your theme please see the [subscribers feature](doc:subscribers) page. Note: if subscribers is not enabled, none of the features on this page will be accessible. ## Routes The `subscribe` context lives at the matching `/subscribe/` route. This cannot be customised at present. ## Templates By default, the `subscribe` context will be rendered using the `subscribe.hbs` file which exists in [/subscribers/lib/views/](https://github.com/TryGhost/Ghost/blob/0.8.0/core/server/apps/subscribers/lib/views/subscribe.hbs). By default, this page is styled to match the Ghost login screen. To customise this page, theme developers can provide their own `subscribe.hbs` file as part of their theme. This will override the default. ## Data There are 3 pieces of data passed to the `subscribe.hbs` template: - **success** (boolean) - is true if this page is being rendered after a successful subscribe - **error** (object) - has a `message` property if something went wrong when the form was submitted - **email** (string) - the email address that was submitted ## Helpers The `subscribe` context makes use of a specialist helper called `{{subscribe_form}}`. Documentation for this helper can be found on the [subscribers feature](doc:subscribers) page. ## Example Code The `subscribe.hbs` template is intended to have a basic structure which displays a different result depending on whether the form has been successfully submitted or not. The following example shows how to display the form if success is false, or otherwise output a success message: [block:code] { "codes": [ { "code": "{{#if success}}\n <header>\n <h1>Subscribed!</h1>\n </header>\n\n <p>\n You've successfully subscribed to <em>{{@blog.title}}</em>\n with the email address <em>{{email}}</em>.\n </p>\n{{else}}\n\t<header>\n <h1>Subscribe to {{@blog.title}}</h1>\n </header>\n\n {{subscribe_form autofocus=\"true\"}}\n{{/if}}", "language": "handlebars" } ] } [/block] The default setup assumes that you'll do any error handling you need within the form itself, so the outer page template can just handle the success / no success case. However, if you want to add some additional template logic based on the error case, it is totally possible to use an 'if - else if - else' structure with handlebars, like this: [block:code] { "codes": [ { "code": "{{#if success}}\n <header>\n <h1>Success!</h1>\n </header>\n{{else if error}}\n <header>\n <h1>Error!</h1>\n </header>\n\n {{subscribe_form autofocus=\"true\"}}\n{{else}}\n <header>\n <h1>Subscribe.../h1>\n </header>\n\n {{subscribe_form autofocus=\"true\"}}\n{{/if}}", "language": "handlebars" } ] } [/block] If you're a big fan of minimising your templates, there's a trick you can use from the express-hbs library that Ghost uses called "content blocks". This allows you to define regions of your template, and define the content for those regions separately, although it requires that the content be defined first to work properly: [block:code] { "codes": [ { "code": "{{#if success}}\n {{#contentFor \"title\"}}Success!{{/contentFor}}\n {{#contentFor \"body\"}}<p>You've successfully subscribed :)</p>{{/contentFor}}\n{{else if error}}\n {{#contentFor \"title\"}}Error!{{/contentFor}}\n {{#contentFor \"body\"}}{{subscribe_form autofocus=\"true\"}}{{/contentFor}}\n{{else}}\n {{#contentFor \"title\"}}Subscribe..{{/contentFor}}\n {{#contentFor \"body\"}}{{subscribe_form autofocus=\"true\"}}{{/contentFor}}\n{{/if}}\n\n<section class=\"subscribe\">\n<header>\n <h1>{{{block \"title\"}}}</h1>\n</header>\n\n{{{block \"body\"}}}\n\n</section>", "language": "handlebars" } ] } [/block] If you're looking for more documentation on the `{{subscribe_form}}` helper, please see the the [subscribers feature](doc:subscribers) page.