Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

allow handlePrefix option to be used with render tag #40

Merged
merged 2 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Fractal = require('@frctl/fractal');
const _ = require('lodash');
const Path = require('path');
const utils = Fractal.utils;
const adapterUtils = require('./utils');

class TwigAdapter extends Fractal.Adapter {

Expand All @@ -23,12 +24,10 @@ class TwigAdapter extends Fractal.Adapter {

Twig.Templates.registerLoader('fractal', function(location, params, callback, errorCallback) {

let parser = Twig.Templates.parsers['twig'];

if (params.precompiled) {
params.data = params.precompiled;
} else {
let view = isHandle(location) ? self.getView(location) : _.find(self.views, {path: Path.join(source.fullPath, location)});
let view = adapterUtils.isHandle(location, self._config.handlePrefix) ? self.getView(location) : _.find(self.views, {path: Path.join(source.fullPath, location)});
if (!view) {

throw new Error(`Template ${location} not found`);
Expand All @@ -52,7 +51,7 @@ class TwigAdapter extends Fractal.Adapter {

let handle = null;

if (isHandle(this.id)) {
if (adapterUtils.isHandle(this.id, self._config.handlePrefix)) {
handle = this.id;
} else {
let view = _.find(self.views, {path: Path.join(source.fullPath, this.id)});
Expand All @@ -62,8 +61,7 @@ class TwigAdapter extends Fractal.Adapter {
}

if (handle) {
let prefixMatcher = new RegExp(`^\\${self._config.handlePrefix}`);
let entity = source.find(handle.replace(prefixMatcher, '@'));
let entity = source.find(adapterUtils.replaceHandlePrefix(handle, self._config.handlePrefix));
if (entity) {
entity = entity.isComponent ? entity.variants().default() : entity;
if (config.importContext) {
Expand Down Expand Up @@ -118,10 +116,6 @@ class TwigAdapter extends Fractal.Adapter {
}

});

function isHandle(str) {
return str && str.startsWith(self._config.handlePrefix);
}
}

get twig() {
Expand Down Expand Up @@ -198,7 +192,7 @@ module.exports = function(config) {
Twig.extendTest(name, test);
});
Twig.extend(function(Twig) {
_.each(require('./tags')(app), function(tag){
_.each(require('./tags')(app, config), function(tag){
Twig.exports.extendTag(tag(Twig));
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/tags/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

module.exports = function(fractal){
module.exports = function(fractal, config){

return {
render: require('./render.js')(fractal)
render: require('./render.js')(fractal, config)
}

};
10 changes: 7 additions & 3 deletions src/tags/render.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

const _ = require('lodash');
const path = require('path');
const utils = require('@frctl/fractal').utils;
const adapterUtils = require('../utils');

/**
* Render tag
*
* Format: {% render "@component" with {some: 'values'} %}
*/
module.exports = function (fractal) {
module.exports = function (fractal, config) {
return function (Twig) {
return {
type: 'rendertag',
Expand Down Expand Up @@ -42,7 +42,11 @@ module.exports = function (fractal) {
},
parse: function (token, context, chain) {
const file = Twig.expression.parse.apply(this, [token.stack, context]);
const handle = path.parse(file).name;
let handle = path.parse(file).name;

if (adapterUtils.isHandle(handle, config.handlePrefix)) {
handle = adapterUtils.replaceHandlePrefix(handle, config.handlePrefix);
}

if (!handle.startsWith('@')) {
throw new Error(`You must provide a valid component handle to the render tag.`);
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = {
isHandle(str, handlePrefix) {
return str && str.startsWith(handlePrefix);
},
replaceHandlePrefix(handle, handlePrefix) {
let prefixMatcher = new RegExp(`^\\${handlePrefix}`);

return handle.replace(prefixMatcher, '@');
},
};