(page, property, context, options)
| 16 | // highter level than `lib/`) how to use the URL to figure out the |
| 17 | // equivalent English page instance. |
| 18 | export async function renderContentWithFallback(page, property, context, options) { |
| 19 | if (!(page instanceof Page)) { |
| 20 | throw new Error(`The first argument has to be Page instance (not ${typeof page})`) |
| 21 | } |
| 22 | if (typeof property !== 'string') { |
| 23 | throw new Error(`The second argument has to be a string (not ${typeof property})`) |
| 24 | } |
| 25 | const template = page[property] |
| 26 | try { |
| 27 | return await renderContent(template, context, options) |
| 28 | } catch (error) { |
| 29 | // Only bother trying to fallback if it was an error we *can* fall back |
| 30 | // on English for. |
| 31 | if (isLiquidError(error) && context.getEnglishPage) { |
| 32 | const enPage = context.getEnglishPage(context) |
| 33 | const englishTemplate = enPage[property] |
| 34 | // If you don't change the context, it'll confuse the liquid plugins |
| 35 | // like `data.js` that uses `environment.scope.currentLanguage` |
| 36 | const enContext = Object.assign({}, context, { currentLanguage: 'en' }) |
| 37 | // Try again! |
| 38 | return await renderContent(englishTemplate, enContext, options) |
| 39 | } |
| 40 | throw error |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Returns the result of executing the first function, but if it fails |
| 45 | // return the result of executing the second function. |
no test coverage detected