Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export all built-in filters as functions for direct use #464

Closed
Nowaker opened this issue Feb 5, 2022 · 6 comments
Closed

Export all built-in filters as functions for direct use #464

Nowaker opened this issue Feb 5, 2022 · 6 comments

Comments

@Nowaker
Copy link
Contributor

Nowaker commented Feb 5, 2022

I must implement date_to_xmlschema from Jekyll and I'd like to call

export function date (this: FilterImpl, v: string | Date, arg: string) {
in my filter directly. This is however not possible because these filters aren't exported by the module.

@Nowaker
Copy link
Contributor Author

Nowaker commented Feb 5, 2022

And being able to use classes like LiquidTime or TimezoneDate would be helpful too. It's essential for any type of modding Liquidjs.

@harttle
Copy link
Owner

harttle commented Feb 5, 2022

I think this makes sense, I can help include this in next version.

github-actions bot pushed a commit that referenced this issue Feb 23, 2022
# [9.35.0](v9.34.1...v9.35.0) (2022-02-23)

### Bug Fixes

* `url_encode` throws on undefined value, fixes [#479](#479) ([ca3240c](ca3240c))

### Features

* expose all tags/filters and TimezoneDate, closes [#464](#464) ([dab8a29](dab8a29))
@github-actions
Copy link

🎉 This issue has been resolved in version 9.35.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

github-actions bot pushed a commit that referenced this issue Mar 2, 2022
## [9.35.2](v9.35.1...v9.35.2) (2022-03-02)

### Bug Fixes

* corner case for concat filter without argument, [#481](#481) ([aa95517](aa95517))
* export all builtin tags from LiquidJS, [#464](#464) ([33009bb](33009bb))
@ghost
Copy link

ghost commented May 13, 2023

How do you call the builtin filters? I'm trying to use default inside of my own filter, so I don't have to do all the same work that default already does. I want to make a filter that only prepends to the value if there is a value...

tplEngine.registerFilter("pre", function pre(value: any, ...args: any[]) {
  const defaultValue = filters["default"] as FilterHandler;
  const valueToPrefix = defaultValue(value, false);
});

I get a TS error for defaultValue saying that The 'this' context of type 'void' is not assignable to method's 'this' of type 'FilterImpl'.ts(2684), so I must be doing something wrong. Where is the impl to call it from?

@ghost
Copy link

ghost commented May 13, 2023

OK, I think I figured it out now. This is working:

import type { FilterHandler } from "liquidjs/dist/src/template/filter-impl-options";
import { Liquid, filters } from "liquidjs";


/** Built-in filter, use in custom filter `append.call(this,...args)` */
const append = filters["append"] as FilterHandler;
/** Built-in filter, use in custom filter `getDefault.call(this,...args)` */
const getDefault = filters["default"] as FilterHandler;
/** Built-in filter, use in custom filter `prepend.call(this,...args)` */
const prepend = filters["prepend"] as FilterHandler;

tplEngine.registerFilter(
  "post",
  /** An "append" filter that only appends when there is a value. */
  function post(value: any, postfix: string) {
    value = getDefault.call(this, value, false);
    if (value === false) return "";
    return append.call(this, value, postfix);
  },
);
tplEngine.registerFilter(
  "pre",
  /** A "prepend" filter that only prepends when there is a value. */
  function pre(value: any, prefix: string) {
    value = getDefault.call(this, value, false);
    if (value === false) return "";
    return prepend.call(this, value, prefix);
  },
);

I also used the following tactic to register a shorter alias to the default filter:

tplEngine.registerFilter("or", getDefault);

I am not sure if there is any difference between accessing the imported filters above or using tplEngine.filters.

@harttle
Copy link
Owner

harttle commented May 13, 2023

You’re right. Use .call() or .apply() to pass a proper this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants