GithubHelp home page GithubHelp logo

Comments (13)

dantti avatar dantti commented on May 28, 2024

Hehe, this is something that I wanted to do a while ago, basically Grantlee stance is to no call object method, so I'm thinking in writting a custon filter like
{{ c|uriFor "action name" }}
Still I dunno if Grantlee exposes API for this, if you how please do a PR, otherwise I might look at this later this week.

from cutelyst.

dantti avatar dantti commented on May 28, 2024

For the moment I have been using relative urls..

from cutelyst.

 avatar commented on May 28, 2024

Yes, a filter was also my first thought. But since the Grantlee documentation is a little bit sparse on that topic I am still investigating. As far as I know one can only add new filters by loading Qt plugins that implement a certain interface.

from cutelyst.

dantti avatar dantti commented on May 28, 2024

It doesn't look so complex http://www.grantlee.org/apidox/extension.html

from cutelyst.

 avatar commented on May 28, 2024

Yeah, I know, I know ;) I already went through this but got stuck at the cmake macros. But I am on it...

from cutelyst.

 avatar commented on May 28, 2024

OK, I don't know what I am doing wrong. I've been trying to build a simple filter that just prefixes a variable with a static string:

myfilter.hpp:

#ifndef __myfilter_hpp                                                      
#define __myfilter_hpp                                                      
                                                                            
#include <grantlee/filter.h>                                                
#include <grantlee/safestring.h>                                            
#include <grantlee/util.h>                                                  
                                                                            
class MyFilter : public Grantlee::Filter {                                  
  QVariant doFilter( const QVariant &input, const QVariant &arg,            
      bool autoescape) const;                                               
                                                                            
  bool isSafe() const { return true; }                                      
};                                                                          
                                                                            
#endif

myfilter.cpp:

#include "myfilter.hpp"                                                     
                                                                            
QVariant MyFilter::doFilter(                                                
    const QVariant &input,                                                  
    const QVariant &arg = QVariant(),                                       
    bool autoescape = false                                                 
  ) const {                                                                
                                                                           
  Grantlee::SafeString str = Grantlee::getSafeString(input);               
  return "MyFilter Says: " + str + "//";                                   
}

mylib.hpp:

#ifndef __mylib_hpp                                                         
#define __mylib_hpp                                                         
                                                                            
#include <QDebug>                                                           
#include <grantlee/taglibraryinterface.h>                                   
                                                                            
#include "myfilter.hpp"                                                     
                                                                            
#define MyFilterLib_iid "de.chronowerks.qtplugins.MyFilterPlugin"           
                                                                            
class MyFilterLib : public QObject, public Grantlee::TagLibraryInterface { 
  Q_OBJECT                                                                  
  Q_PLUGIN_METADATA(IID "de.chronowerks.qtplugins.MyFilterPlugin" FILE "mylib.json")
  Q_INTERFACES(Grantlee::TagLibraryInterface)                               
                                                                            
  public:                                                                   
    MyFilterLib( QObject *parent = 0 ) : QObject(parent) {                  
      m_filters.insert("myfilter", new MyFilter());                         
    }                                                                       
                                                                            
    QHash<QString, Grantlee::Filter*> filters(const QString &name = QString()) {
      Q_UNUSED(name);                                                       
      return m_filters;                                                     
    }                                                                       
};                                                                          
                                                                            
#endif

CMakeLists.txt:

cmake_minimum_required(VERSION 3.7)                                         
project(myfilterlib)                                                        
                                                                            
add_library(myfilterlib MODULE                                              
  myfilter.cpp                                                              
  myfilter.hpp                                                              
  mylib.hpp                                                                 
)                                                                           
                                                                            
find_package(Qt5 COMPONENTS Core REQUIRED)                                  
find_package(Grantlee5 REQUIRED)                                           
                                                                            
set_property(TARGET myfilterlib PROPERTY EXPORT_NAME myfilterlib)          
                                                                            
target_link_libraries(myfilterlib Grantlee5::Templates)                     
grantlee_adjust_plugin_name(myfilterlib)

Everything seems fine to me. The resulting library lib is located in the build directory under grantlee/5.1/myfilterlib.so and builds without error.

In a very simple test program I tried to add the library:

Grantlee::Engine *e = new Grantlee::Engine();                             
e->addPluginPath("plugins");                                              
e->addDefaultLibrary("myfilterlib");

but to no avail:

grantlee.template: "Plugin library 'myfilterlib' not found."

Even though strace confirmed it's been opened:

> grep myfilter trace.txt
stat("[...]plugins/grantlee/5.1/myfilterlib.so", {st_mode=S_IFREG|0755, st_size=19256, ...}) = 0
...
open("[...]plugins/grantlee/5.1/myfilterlib.so", O_RDONLY|O_CLOEXEC) = 4

But I can't see anything wrong with the interface implementation.
Do you have any idea?

from cutelyst.

dantti avatar dantti commented on May 28, 2024

Ok, I finally managed to setup this, you can look at grantlee_uri_for branch, but it seems
that a Filter is not good for uri_for since you can only pass one argument, and requires you to put the Contex variable.

A tag on the other hand could probably be used like:
{% c_uri_for "action" "query" %}
which since has access to the Parser can get the Context automatically.

Grantlee is a project I'm trying to avoid forking but the maintainer doesn't have time to even accept PR, and doesn't seem to be willing to add me as a contributor... I'd like to add an API to not require creating a plugin which has no use outside Cutelyst...

from cutelyst.

 avatar commented on May 28, 2024

I'd like to add an API to not require creating a plugin which has no use outside Cutelyst...

Exactly. That's what I thought.
Maybe there is a possibility to fill c->config() with a hash of the controllers (like in the first posting) so that something like {{c.config.urlfor.Authen.logout}} would be possible...

from cutelyst.

dantti avatar dantti commented on May 28, 2024

it's too much work to build such a hash, not to mention it won't work for chained, I think a tag will be fine.

from cutelyst.

 avatar commented on May 28, 2024

OK

from cutelyst.

dantti avatar dantti commented on May 28, 2024

I have updated the branch with a tag now, please try it, I guess c_uri_for_action will also be needed, and some euristics in parsing arguments.

from cutelyst.

dantti avatar dantti commented on May 28, 2024

And there you go :D

from cutelyst.

 avatar commented on May 28, 2024

Nice! :D Works like a charme!

from cutelyst.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.