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

Logic on head.html #345

Closed
Ices-Eyes opened this issue Nov 15, 2017 · 8 comments
Closed

Logic on head.html #345

Ices-Eyes opened this issue Nov 15, 2017 · 8 comments
Assignees

Comments

@Ices-Eyes
Copy link

Hi,
I would like to ask a question. I wondering if there is any way to put some logic in the head.html file. What I would like to do is to read the persistent storage to check fore some setting and i.e. load some css files or some other based on the setting value. But trying it it says, probably correctly, that the PersistentStorage object is not available! Any way? Or the only way is to put the logic on the function.js and hence loading the file via js?

Thanks :)

@mleroi mleroi self-assigned this Nov 16, 2017
@mleroi
Copy link
Collaborator

mleroi commented Nov 16, 2017

Hi Luca :)
What you need is to use the "template-args" filter (see example here) to pass the PersistentStorage module to the head.html template.

BUT this can't be done from theme's function.js because head.html template (and layout.html) are loaded early in the process and can't be handled from theme (for now). Only WP-AppKit addons can hook early enough to modify things in head or layout templates.

So what you want to do could be done from an addon. A WP-AppKit addon is just a WordPress plugin that hooks into specific WP-AppKit hooks.
If you feel like it I can give you some indications on how to create an addon that would allow to use the PersistentStorage module inside head.html.

@Ices-Eyes
Copy link
Author

Hi, I've seen the documentation for the addon, but it does not say too much to me... I've tried to make one for other purposes, but going nowhere :D
Programming is not a problem, so if there is some hints or docs to see, they are really welcome... :)

@mleroi
Copy link
Collaborator

mleroi commented Nov 16, 2017

Ok great :D

Here's an addon bootstrap that you can use: https://github.com/mleroi/wpak-addon-test.
Used as is, it will just include the wpak-addon-test.js JS file at the beginning of the app initialization process.

To make the PersistentStorage module available in head.html you can modify this wpak-addon-test.js file like so:

define( [ 'core/theme-app', 'core/modules/persistent-storage' ], function ( App, PersistentStorage ) {
	 
	App.filter( 'template-args', function ( template_args, view_type ) {
		if ( view_type === 'head' ) {
			template_args.PersistentStorage = PersistentStorage;
		}
		return template_args;
	} );
	
} );

To install the addon: install it like a regular WordPress plugin, then you'll see it appear in your app edition panel under Addons > WP-AppKit Addon Test > check the checkbox to activate the addon for your app.

Hope that helps,
Don't hesitate for any question.

@Ices-Eyes
Copy link
Author

Ices-Eyes commented Nov 29, 2017 via email

@Ices-Eyes
Copy link
Author

Ices-Eyes commented Dec 1, 2017

Hi Matieu,
made some investingation, and it seems that all is working in the preview but not on the real app on the phone.
The error I obtain in the logcat is this one:
D/SystemWebChromeClient: : Line 39 : ReferenceError: PersistentStorage is not defined

These are the php plugin and js file I had (the js file is on the folder js of the php wordpress plugin folder, but tried to put it also on the js folder of the app:
PHP file:

<?php
if ( !class_exists( 'WpAppKitBootstrapAddon' ) ) {
    class WpAppKitBootstrapAddon {
        const name = 'WP-AppKit Bootstrap Addon';
        const slug = 'wpak-bootstrap-addon';
        public static function hooks() {
            add_filter( 'wpak_addons', array( __CLASS__, 'wpak_register_addon' ) );
        }
        public static function wpak_register_addon( $available_addons ) {
            $addon = new WpakAddon( self::name, self::slug );
            $addon->set_location( __FILE__ );
            $addon->add_js( 'js/wpak-bootstrap-addon.js', 'init' );

            $available_addons[] = $addon;
            return $available_addons;
        }
    }
    WpAppKitBootstrapAddon::hooks();
}
?>

JS File:

define( [ 'core/theme-app', 'core/modules/persistent-storage' ], function ( App, PersistentStorage ) {
	 
	App.filter( 'template-args', function ( template_args, view_type ) {
		if ( view_type === 'head' ) {
			template_args.PersistentStorage = PersistentStorage;
		}
		return template_args;
	} );
} );

Code in the heasd.html:

<% try {
    if( PersistentStorage.get( "settings", "useDarkTheme", false ) === true) { %>
    <link rel="stylesheet" href='<%= TemplateTags.getThemeAssetUrl("css/dark-theme.css") %>'>
<% } } catch ( e ) {} %>

Am I missing something?

Thanks!

@mleroi
Copy link
Collaborator

mleroi commented Dec 4, 2017

Hi Luca,
I've re-tested the same logic on my side (adding PersistentStorage module to head.html via addon) and it worked for me on a real built / real device (Android).
The first thing you can check is if your addon files are in the zip package exported by WP-AppKit, to be sure that your addon sources are really embedded in the app.
Then it will be better if you can write directly to support[at]uncategorized-creations.com with more details (iOS/android, other plugins etc) and maybe your addon and your theme files so that we can find what's going wrong in your case.
Regards

@Ices-Eyes
Copy link
Author

Damn!!! I've found the problem!!!
I have a folder with the files I use to create the zip for phonegap, where I have made various configurations for the apk, like screenshots, modifications to the config file, and so on.
When I download the zip from WPappkit plugin, i just copied the theme folder to the one I have local and then create the zip. But this time I completely forgot to copy also the wpappkit addon folder!
Sorry to disturb you. My fault this time :S
Thanks,
Luca

@mleroi
Copy link
Collaborator

mleroi commented Dec 7, 2017

Hi Luca, no problem, we know the export/build/config process can be tricky ;)
Good to know you've solved your problem!

@mleroi mleroi closed this as completed Dec 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants