jekyll / liquid:从模板中的哈希给出密钥访问值

时间:2016-07-15 12:47:43

标签: jekyll liquid

在jekyll模板中给出以下代码

{% assign resource = site.data.resources | where: "id", page.resource %}

派生以下哈希:

{
  "id"=>"resource-1234", 
  "title"=>"testing", 
  "description"=>"Quis autem vel eum iure reprehenderit qui"
}

如何使用liquid来输出标题键的值?我尝试过以下方法:

{{ resource }}     # outputs the hash
{{ resource.title }}   # nil
{{ resource["title"] }}   # nil

1 个答案:

答案 0 :(得分:5)

实际上,# gitlab-rake db:migrate == 20160301124843 AddVisibilityLevelToGroups: migrating ======================= -- add_column(:namespaces, :visibility_level, :integer, {:null=>false, :default=>20}) -> 0.0985s -- add_index(:namespaces, :visibility_level) -> 0.0057s -- select_one("SELECT restricted_visibility_levels FROM application_settings ORDER BY id DESC LIMIT 1") -> 0.0005s rake aborted! StandardError: An error has occurred, this and all later migrations canceled: undefined method `<' for nil:NilClass/opt/gitlab/embedded/service/gitlab-rails/db/migrate/20160301124843_add_visibility_level_to_groups.rb:9:in `up' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:611:in `exec_migration' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:592:in `block (2 levels) in migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:591:in `block in migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:590:in `migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:768:in `migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:1044:in `block in ddl_transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:1044:in `ddl_transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:997:in `execute_migration_in_transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:959:in `block in migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `each' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:823:in `up' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:801:in `migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:137:in `migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>' NoMethodError: undefined method `<' for nil:NilClass /opt/gitlab/embedded/service/gitlab-rails/db/migrate/20160301124843_add_visibility_level_to_groups.rb:9:in `up' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:611:in `exec_migration' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:592:in `block (2 levels) in migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:591:in `block in migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:590:in `migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:768:in `migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:1044:in `block in ddl_transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:1044:in `ddl_transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:997:in `execute_migration_in_transaction' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:959:in `block in migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `each' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:823:in `up' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:801:in `migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:137:in `migrate' /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>' Tasks: TOP => db:migrate 过滤器会返回一个数组。

当您使用where打印此数组时,它只会依次输出所有项目。这里打印你的哈希值,这让你认为{{ resource }}是一个哈希值。

要进行调试,您可以使用将返回的resource

{{ resource | inspect }}

在这里你看到括号,你知道[{"id"=>"resource-1234", "title"=>"testing", "description"=>"Quis ..."}] 是一个数组。

在您的情况下,您知道只有一个(或零)资源链接到您的页面。为了只获取第一个资源哈希,你可以这样做:

resource

现在{% assign resource = site.data.resources | where: "id", page.resource | first %} 会返回{{ resource.title }}

酷不是吗?